使用 selenium python 下载图像 [英] Download image with selenium python

查看:40
本文介绍了使用 selenium python 下载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从浏览器获取验证码图像.我有这张图片的网址,但这张图片每次更新都会改变(网址不变).

I want get captcha image from browser. I have got a url of this picture, but the this picture changes each updated time (url is constant).

有没有办法从浏览器中获取图片(比如图片另存为"按钮)?

Is there any solution to get picture from browser (like 'save picture as' button)?

另一方面,我认为它应该是工作:

From the other hand, I think it should be work:

  1. 获取浏览器截图
  2. 获取图片位置
  3. 使用 opencv 从屏幕截图中裁剪验证码

动态验证码链接 - 链接

问题已通过截图解决:

browser.save_screenshot('screenshot.png')
img = browser.find_element_by_xpath('//*[@id="cryptogram"]')
loc = img.location

image = cv.LoadImage('screenshot.png', True)
out = cv.CreateImage((150,60), image.depth, 3)
cv.SetImageROI(image, (loc['x'],loc['y'],150,60))
cv.Resize(image, out)
cv.SaveImage('out.jpg', out)

谢谢

推荐答案

这是一个完整的例子(使用 google 的 recaptcha 作为目标):

Here's a complete example (using google's recaptcha as a target):

import urllib
from selenium import webdriver

driver = webdriver.Firefox()
driver.get('http://www.google.com/recaptcha/demo/recaptcha')

# get the image source
img = driver.find_element_by_xpath('//div[@id="recaptcha_image"]/img')
src = img.get_attribute('src')

# download the image
urllib.urlretrieve(src, "captcha.png")

driver.close()

更新:

动态生成图像的问题是每次请求都会生成一个新图像.在这种情况下,您有多种选择:

The problem with dynamic generated images is that there is a new image generated each time you request it. In that case, you have several options:

  • 截图

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('https://moscowsg.megafon.ru/ps/scc/php/cryptographp.php?PHPSESSID=mfc540jkbeme81qjvh5t0v0bnjdr7oc6&ref=114&w=150')

driver.save_screenshot("screenshot.png")

driver.close()

  • 模拟右键单击+另存为".有关详细信息,请参阅此主题.

    希望有所帮助.

    这篇关于使用 selenium python 下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆