正在使用 selenium/urllib 从网络下载 python 3 中的图像文件

Downloading image file in python 3 from web using selenium/urllib

正在尝试下载验证码图像。

出现以下错误: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

这是您可以用来保存 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)