简单的 Python 使用保持 Session 签到 [ 技术分享 ]
# coding=utf-8
import requests
from pyquery import PyQuery as pq
#安装插件
# pip3.7 install requests
# pip3.7 install PyQuery
# 登录地址
login_url = "https://www.yiichina.com/login"
# 签到地址
url2 = "https://www.yiichina.com/registration"
# 构造登陆的header头
headers = {
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
'Accept-Encoding': "gzip, deflate, br",
'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
'Cache-Control': "no-cache",
'Connection': "keep-alive",
'Content-Length': "250",
'Content-Type': "application/x-www-form-urlencoded",
'Host': "www.yiichina.com",
'Origin': "https://www.yiichina.com",
'Pragma': "no-cache",
'Referer': "https://www.yiichina.com/login",
'Upgrade-Insecure-Requests': "1",
'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/67.0.3396.99 Safari/537.36 "
}
# 构造签到的header头
headers2 = {
'Accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
'Accept-Encoding': "gzip, deflate, br",
'Accept-Language': "zh-CN,zh;q=0.9,en;q=0.8",
'Cache-Control': "no-cache",
'Connection': "keep-alive",
'Content-Length': "250",
'Content-Type': "application/x-www-form-urlencoded",
'Host': "www.yiichina.com",
'Origin': "https://www.yiichina.com",
'Pragma': "no-cache",
'Referer': "https://www.yiichina.com/",
'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/67.0.3396.99 Safari/537.36",
'X-CSRF-Token': "jiZGgvngdp0HiiclI4SheJlJr5-lVyymIFgbfE1ZUw33FWv2mI5DzyrJFUFHt8tO_h3az84GH9xEHEtFCh8cfg==",
'X-Requested-With': "XMLHttpRequest"
}
# 登陆方法和签到
def login_sign():
r_session = requests.Session()
# 请求页面
page = r_session.get(login_url)
# 请求获得的内容
content = pq(page.content)
# 获取csrf_token
item1 = content('meta[name=csrf-token]').items()
csrf = ''
for nurl in item1:
csrf = nurl.attr('content')
data = {
"_csrf": csrf,
"LoginForm[username]": "xxxxxx",#帐号
"LoginForm[password]": "xxxxxx",#密码
'LoginForm[rememberMe]': "0",
"LoginForm[rememberMe]": "1",
"login - button": ""
}
# 完成用户登录
response1 = r_session.post(login_url, data=data, headers=headers)
# 请求获得的内容
content = pq(response1.content)
# 获取csrf_token
item1 = content('meta[name=csrf-token]').items()
csrf = ''
for nurl in item1:
csrf = nurl.attr('content')
data2 = {
"_csrf": csrf
}
# 模拟签到请求
response2 = r_session.post(url2, data=data2, headers=headers2)
content = pq(response2.content)
items = content('p').items()
for a in items:
print(a.html())
if __name__ == "__main__":
login_sign()
共 3 条回复
tinymeng 北京
注册时间:2018-03-05
最后登录:2024-10-24
在线时长:36小时43分
最后登录:2024-10-24
在线时长:36小时43分
- 粉丝7
- 金钱25065
- 威望20
- 积分25625