【问题标题】:Save an image with selenium?用硒保存图像?
【发布时间】:2019-09-19 17:10:02
【问题描述】:

这是我要下载的源代码:

<div id="image-captcha-section" class="a-section a-spacing-large">
  <input type="hidden" name="use_image_captcha" value="true" id="use_image_captcha">
  <div class="a-section a-spacing-base">
    <h4>
      Enter the characters you see
    </h4>

<div id="auth-captcha-image-container" class="a-section a-text-center">
  <img alt="Visual CAPTCHA image, continue down for an audio option." src="https://opfcaptcha-prod.s3.amazonaws.com/b2ebf54f0e9e48ca8e4e8552974565c1.jpg?AWSAccessKeyId=AKIA5WBBRBBB3EQNKAH5&amp;Expires=1568911776&amp;Signature=BQzylyeOezMXtuLXgnfY3IX0CdA%3D" data-refresh-url="/ap/captcha?appAction=REGISTER&amp;captchaObfuscationLevel=ape%3AZWFzeQ%3D%3D&amp;captchaType=image&amp;marketPlaceId=A1EVAM02EL8SFB" id="auth-captcha-image">
</div>

这是我的代码:

                    img = driver.find_element_by_xpath('//div[@id="auth-captcha-image"]')
                    src = img.get_attribute('src')
                    # download the image
                    urllib.urlretrieve(src, "captcha.jpg")
                    print("Done")

我搜索过其他主题,但无法保存此图片,网址为:https://opfcaptcha-prod.s3.amazonaws.com/b2ebf54f0e9e48ca8e4e8552974565c1.jpg

谢谢你的帮助!

【问题讨论】:

  • “但无法保存此图像” - 为什么不能呢?当你尝试时会发生什么?
  • 您使用的是哪个版本的python。它在 python 3.4 及更高版本中被禁用。
  • 嗨,我使用的是 Python 3.7.4。那么我该如何解决?降级python?

标签: python selenium python-3.7


【解决方案1】:

试试这个 Alternative of urllib.urlretrieve in Python 3.5 如果您使用的是 python 3.5 及更高版本,这可能是正确的方法

【讨论】:

  • 我正在使用 Python 3.7.4
  • 链接有帮助吗?
【解决方案2】:

您选择的标签名称是div,但它是一个img 标签。因此请更改您的xpath 以定位该元素。

img = driver.find_element_by_xpath('//img[@id="auth-captcha-image"]')
src = img.get_attribute('src')

【讨论】:

    猜你喜欢
    • 2021-01-19
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2016-06-29
    • 2015-06-13
    • 2013-11-08
    相关资源
    最近更新 更多