使用 selenium python 下载图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17361742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download image with selenium python
提问by user1941407
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:
另一方面,我认为它应该是工作:
- get screenshot of the browser
- get position of picture
- crop captcha from screenshot using opencv
- 获取浏览器的截图
- 获取图片位置
- 使用 opencv 从屏幕截图中裁剪验证码
link of the dynamic capcha - link
The problem was solved via screenshot:
问题通过截图解决:
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)
Thanks
谢谢
回答by alecxe
Here's a complete example (using google's recaptcha as a target):
这是一个完整的示例(使用 google 的 recaptcha 作为目标):
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()
UPDATE:
更新:
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:
动态生成图像的问题是每次请求都会生成一个新图像。在这种情况下,您有多种选择:
take a screenshot
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()
simulate right click + "Save As". See this threadfor more info.
截屏
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()
模拟右键单击+“另存为”。有关更多信息,请参阅此线程。
Hope that helps.
希望有帮助。
回答by Ramon
It's ok to save a screenshot from the whole page and then cut the image from, but you can also to use the "find" method from "webdriver" to locate the image you want to save, and write the "screenshot_as_png" property like below:
可以保存整个页面的屏幕截图然后从中剪切图像,但您也可以使用“webdriver”中的“find”方法来定位要保存的图像,并编写如下所示的“screenshot_as_png”属性:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.webpagetest.org/')
with open('filename.png', 'wb') as file:
file.write(driver.find_element_by_xpath('/html/body/div[1]/div[5]/div[2]/table[1]/tbody/tr/td[1]/a/div').screenshot_as_png)
Sometimes it could get an error because of the scroll, but depending on your necessity, it's a good way to get the image.
有时它可能会因为滚动而出错,但根据您的需要,这是获取图像的好方法。