gpt4 book ai didi

python - 错误 : File could not be downloaded from url: 2Cpatcha Api

转载 作者:行者123 更新时间:2023-12-05 04:20:27 26 4
gpt4 key购买 nike

我正在尝试使用 2Captcha python API 解决正常的验证码问题,但它给出了无法下载文件的错误。我不知道为什么会这样,因为我可以从浏览器手动下载它并另存为 .png 来下载它。下面是代码

import sys
import os

sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

from twocaptcha import TwoCaptcha


solver = TwoCaptcha(apikey)

try:
result = solver.normal('https://v2.gcchmc.org/captcha/image/aa699f305917812978c911e87ab126a782f726e7/')

except Exception as e:
sys.exit(e)

else:
sys.exit('solved: ' + str(result))

我还尝试使用请求下载文件,然后将其提供给 API,但同样显示错误。请求的代码是

url = 'https://v2.gcchmc.org/captcha/image/aa699f305917812978c911e87ab126a782f726e7/'
import requests
from PIL import Image
from io import BytesIO
response = requests.get(url)
img = Image.open(BytesIO(response.content)) # error occurs here
img.save('output.png')

错误是

raise UnidentifiedImageError(
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x00000141224AB2C0>

如果有人可以帮助我使用脚本下载图像,我将不胜感激。验证码显示在以下网址上: https://v2.gcchmc.org/book-appointment/

最佳答案

你的代码没问题,是headers引起的问题。该网址需要您提供 header ,而您没有提供 header 。这会导致 PIL 库无法理解的错误响应。工作代码将是

url = 'https://v2.gcchmc.org/captcha/image/aa699f305917812978c911e87ab126a782f726e7/'
import requests
from PIL import Image
from io import BytesIO
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:107.0) Gecko/20100101 Firefox/107.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
# 'Accept-Encoding': 'gzip, deflate, br',
'DNT': '1',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'Sec-Fetch-Dest': 'document',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-User': '?1',
}
response = requests.get(url, headers)
img = Image.open(BytesIO(response.content)) # error occurs here
img.save('output.png')

关于python - 错误 : File could not be downloaded from url: 2Cpatcha Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74455872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com