【问题标题】:Downloading image file in python 3 from web using selenium/urllib使用 selenium/urllib 从 web 下载 python 3 中的图像文件
【发布时间】:2020-02-29 22:15:23
【问题描述】:

尝试下载验证码图像。

出现以下错误:urllib.error.HTTPError:HTTP 错误 500:内部服务器错误

见下面的代码:

from selenium import webdriver
import urllib.request

driver = webdriver.Chrome()

driver.get('https://servicesenligne2.ville.montreal.qc.ca/sel/evalweb/index')

img = driver.find_element_by_xpath('//*[@id="type_recherche"]/div[5]/div/img')
src = img.get_attribute('src')

urllib.request.urlretrieve(src, "captcha.png")

当我打印 src 时,我得到以下信息:

DevTools listening on ws://127.0.0.1:65317/devtools/browser/36eb75bc-f03c-41ee-96cc-138df591c665
https://servicesenligne2.ville.montreal.qc.ca/sel/evalweb/createimage.png?timestamp=1583199024767

【问题讨论】:

  • 你能尝试打印出src的值吗?这是可以传递给urllib.request.urlretrieve的东西吗?
  • @SiKing 我编辑了问题以反映 src 返回的内容。

标签: python-3.x xml selenium urllib


【解决方案1】:

这里是您可以用来保存 captcha.jpg 的示例脚本。

import requests
import shutil
url = "https://servicesenligne2.ville.montreal.qc.ca/sel/evalweb/createimage.png?timestamp=1583203496087"
# we are able to use the same cookie even after refreshing (so you should be good to use the same cookie)
headers = {
  'Cookie': 'JSESSIONID=YB-eSDCWKU-SG_bKEtluH8kzvWMop4B0plLN4NOLXtO09plZSEuS!-209918963'
}

response = requests.get(url,headers=headers, stream=True)
with open("captcha.jpg", 'wb') as f:
    response.raw.decode_content = True
    shutil.copyfileobj(response.raw, f)

下面是完整的代码。

from selenium import webdriver
import requests
import shutil

driver = webdriver.Chrome()

driver.get('https://servicesenligne2.ville.montreal.qc.ca/sel/evalweb/index')

img = driver.find_element_by_xpath('//*[@id="type_recherche"]/div[5]/div/img')
src = img.get_attribute('src')
jsession = driver.get_cookie('JSESSIONID')['value']
headers = {
  'Cookie': 'JSESSIONID='+jsession
}

response = requests.get(src,headers=headers, stream=True)
with open("captcha.jpg", 'wb') as f:
    response.raw.decode_content = True
    shutil.copyfileobj(response.raw, f)

【讨论】:

  • 进步!此代码允许我下载验证码。唯一的问题是,当我打开文件时,它显示“我们似乎不支持这种文件格式”
  • 文件大小是多少?如果它是 0 KB,那么请检查为您生成的 cookie。
  • 您能否尝试使用更新后的代码。我正在动态获取JSESSIONID,因此您不必再担心了。
猜你喜欢
  • 2011-12-16
  • 2018-08-03
  • 2011-06-13
  • 1970-01-01
  • 2011-11-06
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多