# 1.因为用户名和密码和验证码的name都是随机的,所以我们先要获取网页源代码,
#然后提取出其中的name值
# 2.获取到name和once的值以后,再通过调用https://www.v2ex.com/sigin接口,把数据
#通过post请求发送过去
# 3.还需要使用云打码平台去自动识别验证码
import requests
from lxml import etree
from 反爬虫攻防实战.验证码识别.fateadm_api import recognize_captcha
login_url = "https://www.v2ex.com/signin"
settings_url = ''
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
"referer": "https://www.v2ex.com/signin",
}
session = requests.Session()
resp = session.get(login_url,headers=headers)
html = resp.text
parser = etree.HTML(html)
inputs = parser.xpath("//form[@action='/signin']//input")
userInput = inputs[0]
passwordInput = inputs[1]
captchaInput = inputs[2]
onceInput = inputs[3]
# for input in inputs:
# input_text = etree.tostring(input)
# print(input_text)
# print("="*30)
userName = userInput.get('name')
passwordName = passwordInput.get('name')
captchaName = captchaInput.get('name')
onceValue = onceInput.get('value')
data = {
userName:'Lauquan',
passwordName:'你的密码',
"once":onceValue,
'next':'/'
}
captcha_url = "https://www.v2ex.com/_captcha?once="+onceValue
imgResp = session.get(captcha_url,headers=headers)
with open("captcha.png",'wb') as fp:
fp.write(imgResp.content)
# captchaValue = input("请输入验证码:")
captchaValue = recognize_captcha("captcha.png","20900")
data[captchaName] = captchaValue
loginResp = session.post(login_url,headers=headers,data=data)
settingsResp = session.get(settings_url,headers=headers)
print(settingsResp.text)
验证码自动识别登录
最新推荐文章于 2024-07-31 20:06:23 发布