Skip to main content
 首页 » 编程设计

Python爬虫验证图片

2025年04月02日13cmt

我想爬取一张验证图片,我是这样用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_KEY和time)

运行良好,将验证图片保存为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) 

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