1、将验证码下载至本地
url = 'http://www.example.com/a.html'
resp = requests.get(url)
soup = BeautifulSoup(resp.content.decode('UTF-8'), 'html.parser')
src = soup.select_one('div.captcha-row img')['src']
resp = requests.get(src)
with open('../images/verify.png', 'wb') as f:
f.write(resp.content)
2、解析验证码
pip install ddddocr
ocr = ddddocr.DdddOcr()
with open('../images/verify.png', 'rb') as f:
img = f.read()
code = ocr.classification(img)
print(code)
3、发送验证码
token = soup.find('input', {'name': 'csrfToken'}).get('value')
verify_url = 'https://www.example.com/verify'
data = {
'vcode': code,
'token': token,
'btnPost':''
}
headers = {
'content-type': 'application/x-www-form-urlencoded',
'user-agent': 'Mozilla/5.0 (Macintosh;) AppleWebKit/537.36 (KHTML, like Gecko) Chrome'
}
response = requests.post(verify_url, data=data, headers=headers)
if response.status_code == 200:
print('人机验证 - success')
else:
print('人机验证 - fail')