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

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

问题描述

我想从浏览器中获取验证码图片。我有这个图片的网址,但这张图片更改每个更新的时间(网址是恒定的)。

是否有任何解决方案来从浏览器中获取图片(如保存图片为按钮)?从另一方面来说,我认为这应该是工作:


  1. get 浏览器的屏幕截图

  2. 获取图片的位置

  3. 从截屏使用opencv裁剪captcha

动态CAPCHA的链路 - 链接



这个问题是通过屏幕截图解决的:

 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 ',出)

谢谢

解决方案

一个完整的例子(使用谷歌的recaptcha作为目标):

 从selenium导入urllib 
导入webdriver

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

#获取图像源
img = driver.find_element_by_xpath('// div [@ id =recaptcha_image] / img')
src = img.get_attribute('src')

#download
urllib.urlretrieve(src,captcha.png)

driver.close()

更新:

动态生成图像的问题是每次请求时都会生成一个新图像。在这种情况下,你有几个选择:
$ b $ ul

  • 截图

     来自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)

    驱动程序。 close()


  • 模拟右键点击另存为。有关更多信息,请参阅此主题




  • 希望有帮助。


    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. get screenshot of the browser
    2. get position of picture
    3. crop captcha from screenshot using 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

    解决方案

    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()
    

    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 thread for more info.

    Hope that helps.

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

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