古诗文网登录
程序员文章站
2022-03-10 23:09:02
文章目录1. 模拟登录2. 参考源码1. 模拟登录https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx第一第二两个参数可以从登录页源码里获取验证码通过打码平台解决2. 参考源码import reimport requestsfrom cjy import Chaojiying_Clientclass Login(object): def __init__(se...
1. 模拟登录
https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx
第一第二两个参数可以从登录页源码里获取
验证码通过打码平台解决
2. 参考源码
import re
import requests
from cjy import Chaojiying_Client
class Login(object):
def __init__(self):
# 登录地址
self.login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'
# 验证码地址
self.captcha_url = 'https://so.gushiwen.cn/RandCode.ashx'
self.headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}
# 验证码每次刷新都不一样,所以需要通过session保持在同一会话
self.session = requests.Session()
def get_captcha(self):
"""
获取保存验证码图片
:return:
"""
response = self.session.get(self.captcha_url).content
with open('./captcha.jpg', 'wb') as fp:
fp.write(response)
def get_captcha_str(self):
"""
超级鹰打码平台识别验证码
:return:
"""
chaojiying = Chaojiying_Client('xxxx', 'xxxx', 'xxx') # 用户中心>>软件ID 生成一个替换 96001
im = open('captcha.jpg', 'rb').read() # 本地图片文件路径 来替换 a.jpg 有时WIN系统须要//
captcha_str = chaojiying.PostPic(im, 1902)['pic_str'] # 1902 验证码类型
return captcha_str
def get_params(self):
"""
网页源码获取参数
:return:
"""
params_response = self.session.get(self.login_url).text
VIEWSTATE = \
re.findall(r'<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />', params_response)[0]
VIEWSTATEGENERATOR = \
re.findall(r'<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="(.*?)" />',
params_response)[0]
return VIEWSTATE, VIEWSTATEGENERATOR
def login(self, VIEWSTATE, VIEWSTATEGENERATOR, captcha_str):
"""
登录
:param VIEWSTATE:
:param VIEWSTATEGENERATOR:
:param captcha_str:
:return:
"""
data = {
'__VIEWSTATE': VIEWSTATE,
'__VIEWSTATEGENERATOR': VIEWSTATEGENERATOR,
'from': 'http://so.gushiwen.cn/user/collect.aspx',
'email': 'xxxx',
'pwd': 'xxxx',
'code': captcha_str,
'denglu': '登录'
}
response = self.session.post(self.login_url, data=data, headers=self.headers).text
if '163.com' in response:
print('登录成功')
else:
print('登录失败')
def run(self):
self.get_captcha()
captcha_str = self.get_captcha_str()
VIEWSTATE, VIEWSTATEGENERATOR = self.get_params()
self.login(VIEWSTATE, VIEWSTATEGENERATOR, captcha_str)
if __name__ == '__main__':
s = Login()
s.run()
本文地址:https://blog.csdn.net/gklcsdn/article/details/107256100
上一篇: MySQL日期函数与时间函数汇总(MySQL 5.X)
下一篇: 简易UI