Python爬虫验证图片

Python爬虫验证图片,python,curl,web-crawler,Python,Curl,Web Crawler,我想抓取一张验证图片,我已经通过像这样使用curl实现了它 curl "https://www.ris.gov.tw/apply/captcha/image?CAPTCHA_KEY=71cc3b094e824446873038401ab8c303&time=1464968502855" -H "Referer: https://www.ris.gov.tw/id_card/" --insecure >> a.jpg (每次都需要创建验证码和时间) 它工作正常,并将验证图片保

我想抓取一张验证图片,我已经通过像这样使用curl实现了它

curl "https://www.ris.gov.tw/apply/captcha/image?CAPTCHA_KEY=71cc3b094e824446873038401ab8c303&time=1464968502855" -H "Referer: https://www.ris.gov.tw/id_card/" --insecure >> a.jpg
(每次都需要创建
验证码
时间

它工作正常,并将验证图片保存到
a.jpg

现在我正试图用python重写,下面是我所做的

import requests
from bs4 import BeautifulSoup
from datetime import datetime
import shutil
import time
from IPython.display import Image
from random import randint

ori = requests.get("https://www.ris.gov.tw/id_card/")
soup = BeautifulSoup(ori.text)
key =  soup.select('#captchaKey')[0]["value"]
#Get CAPTCHA_KEY 
rs = requests.session()
url = "https://www.ris.gov.tw/apply/captcha/image?CAPTCHA_KEY=" + key
time =  str(int((time.time())*100)) + str(randint(0,9))
url += "&time=" + time
#Get time 

res = rs.get(url, headers={'referer': 'https://www.ris.gov.tw/id_card/'}, stream = True, verify =False)

f= open('check.jpg','wb')
shutil.copyfileobj(res.raw,f)
f.close()
Image('check.jpg')

我被卡住了一段时间,不知道如何解决它。

这些更改为我提供了JPEG文件:

res = rs.get(url, headers={'referer': 'https://www.ris.gov.tw/id_card/'})
with open('check.jpg', 'wb') as jpeg_file:
    jpeg_file.write(res.content)

这是可以直接写入文件的字节响应。

那么,您是收到错误还是什么?它没有得到验证图片,而且
检查.jpg
似乎不是图片格式,我无法打开它。如果您需要帮助找出哪里出了问题,你应该提供一个我知道的…但我现在很难决定我需要提供什么,因为作为一个初学者,我担心我会发布太少,错过一些重要的部分。我会尽力修改的,谢谢。你为什么用png呢?