错误:无法从url: 2patcha Api下载文件



我正在尝试使用2Captcha python API解决正常的captcha,但它给出了文件无法下载的错误。我不知道为什么会发生这种情况,因为我可以从浏览器手动下载它,并保存为。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')

Error is

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

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

你的代码很好,这是头引起的问题。url需要你的头,而你没有提供头。这会导致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')

相关内容

  • 没有找到相关文章

最新更新