验证码操作

Posted xujinjin18

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了验证码操作相关的知识,希望对你有一定的参考价值。

 

 

#下载验证码到本地
import requests
from lxml import etree
import urllib.request

s = requests.session()

url = https://so.gushiwen.org/user/login.aspx?from=http://so.gushiwen.org/user/collect.aspx
headers = {
    user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/69.0.3497.100 Safari/537.36
}

page_content = s.get(url,headers=headers).text

tree = etree.HTML(page_content)
img_url = tree.xpath(//img[@id="imgCode"]/@src)[0]
img_url = https://so.gushiwen.org + img_url

#urllib.request.urlretrieve(img_url,‘./code.png‘)
img_data = s.get(url=img_url,headers=headers).content
with open(./code.png,wb) as fp:
    fp.write(img_data) 

#解析form表单的令牌数据(反爬手段)
key_1 = tree.xpath(//*[@id="__VIEWSTATE"]/@value)[0]
key_2 = tree.xpath(//*[@id="__VIEWSTATEGENERATOR"]/@value)[0]

#step 2:抓包获取登录url
code = input(查看下验证码内容:)
post_url = https://so.gushiwen.org/user/login.aspx?from=http%3a%2f%2fso.gushiwen.org%2fuser%2fcollect.aspx
data = {
    "__VIEWSTATE":key_1,
    "__VIEWSTATEGENERATOR":key_2,
    "from":"http://so.gushiwen.org/user/collect.aspx",
    "email":"[email protected]",
    "pwd":"123456",
    "code":code,
    "denglu":"登录"
}
page_content = s.post(url=post_url,headers=headers,data=data).text

with open(./second.html,w,encoding=utf-8) as fp:
    fp.write(page_content)
print(over)

 

以上是关于验证码操作的主要内容,如果未能解决你的问题,请参考以下文章

git动态验证码

Android SMS Verification API 结果码始终为 0

爬虫遇到头疼的验证码?Python实战讲解弹窗处理和验证码识别

JavaScript 有用的代码片段和 trick

图片识别之验证码识别

微信小程序中短信验证码登录全流程及代码