Python 模拟验证码登陆
Python 模拟验证码登陆
- 获取登录请求
- 打开preserve log
- 点击登录,获取登录请求(post)
- 验证码地址可变
- 爬取页面验证码地址,获取验证码内容
- 将data进行post请求
- 验证码地址不变,而内容随机变化
-
设置session进行验证码的get请求并下载图片进行识别得到验证码的识别结果,再利用这个sesson进行post请求,把账号密码和验证码识别结果的表单数据进行post从而模拟登录
- 如果请求中产生了cookie,则该cookie会被自动存储/携带在该session对象中
- session可以进行请求的发送
-
sesson =requests.session()
code_img_data = sesson.get(url=url,headers=headers).content
#
#
#
response = sesson.post(url = log_url,headers = headers,data=data)
模拟古诗词网登录
codet.py:
#!/usr/bin/env python
# coding:utf-8
import requests
from hashlib import md5
class Chaojiying_Client(object):
def __init__(self, username, password, soft_id):
self.username = username
password = password.encode('utf8')
self.password = md5(password).hexdigest()
self.soft_id = soft_id
self.base_params = {
'user': self.username,
'pass2': self.password,
'softid': self.soft_id,
}
self.headers = {
'Connection': 'Keep-Alive',
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
}
def PostPic(self, im, codetype):
"""
im: 图片字节
codetype: 题目类型 参考 http://www.chaojiying.com/price.html
"""
params = {
'codetype': codetype,
}
params.update(self.base_params)
files = {'userfile': ('ccc.jpg', im)}
r = requests.post('http://upload.chaojiying.net/Upload/Processing.php', data=params, files=files, headers=self.headers)
return r.json()
def ReportError(self, im_id):
"""
im_id:报错题目的图片ID
"""
params = {
'id': im_id,
}
params.update(self.base_params)
r = requests.post('http://upload.chaojiying.net/Upload/ReportError.php', data=params, headers=self.headers)
return r.json()
#
# if __name__ == '__main__':
# chaojiying = Chaojiying_Client('超级鹰用户名', '超级鹰用户名的密码', '96001') #用户中心>>软件ID 生成一个替换 96001
# im = open('a.jpg', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
# print chaojiying.PostPic(im, 1902) #1902 验证码类型 官方网站>>价格体系 3.4+版 print 后要加()
main.py:
from codet import Chaojiying_Client
import requests
from lxml import etree
if __name__ == '__main__':
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
#利用session保存cookie信息
sesson =requests.session()
#登录界面
log_page_text = sesson.get(url=url,headers=headers).text
tree = etree.HTML(log_page_text)
#验证码图片链接
code_img_url = 'https://so.gushiwen.cn'+tree.xpath('//img[@id="imgCode"]/@src')[0]
print(code_img_url)
#获取验证码内容,每次请求验证码内容都会更改,利用session保存最后一次更新的cookie
code_img_data = sesson.get(code_img_url).content
with open('code.jpg','wb') as fp:
fp.write(code_img_data)
chaojiying = Chaojiying_Client('wi0i0i', '********', '920413') #用户中心>>软件ID 生成一个替换 96001
im = open('code.jpg', 'rb').read() #本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
#print (chaojiying.PostPic(im, 1902) )
pic_str = chaojiying.PostPic(im, 1902)['pic_str']
print(pic_str)
#点击登录按钮,产生的请求
headers = {
'referer': 'https: // so.gushiwen.cn / user / login.aspx?from=http: // so.gushiwen.cn / user / collect.aspx',
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}
log_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'
data = {
'__VIEWSTATE': 'AVP2UqKeeCzJep4Lb05tbFRvGOQjFCxVHE / EFBHpF + W + +3eo2R8m6hLLw + FLJNXGm1wVguwWRixwR3U84ig2ifnQJyjcBkLZdLzBWvC7t5XiZvnXcLnVaofhEtk =',
'__VIEWSTATEGENERATOR': 'C93BE1AE',
'from': 'http://so.gushiwen.cn/user/collect.aspx',
'email': '3059519959@qq.com',
'pwd': '*********',
'code': pic_str,
'denglu': '登录'
}
#利用session保存的cookie,保持会话,保证验证码内容一致,
response = sesson.post(url = log_url,headers = headers,data=data)
print(response.status_code)
with open('log.html','w',encoding='utf-8') as fp:
fp.write(response.text)
- 代理
- 请求参数- proxies =
本文来自博客园,作者:w0000,转载请注明原文链接:https://www.cnblogs.com/w0000/p/15097685.html