Python爬虫验证图片

时间:2016-06-03 15:59:40

标签: 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

P.S(每次都需要创建CAPTCHA_KEYtime

工作正常并将验证图片保存到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')

我被困了一段时间,不知道该怎么弄清楚。

1 个答案:

答案 0 :(得分:1)

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

unhex()

content是作为字节的响应,可以直接写入文件。

相关问题