Skip to content

Commit a1c1bce

Browse files
authored
Create 51dalili.py
1 parent 1e9a1e9 commit a1c1bce

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

51dalili.py

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#51代理每日签到
2+
import requests
3+
from bs4 import BeautifulSoup
4+
import time
5+
import os
6+
7+
def login_to_51daili():
8+
# 从环境变量读取账号和密码
9+
username = os.getenv('dali51user')
10+
password = os.getenv('daili51pass')
11+
12+
if not username or not password:
13+
print("错误: 请设置环境变量 dali51user 和 daili51pass")
14+
return None
15+
16+
# 第一次请求获取登录令牌和PHPSESSID
17+
print("正在获取登录令牌和PHPSESSID...")
18+
session = requests.Session()
19+
20+
try:
21+
# 初始headers,仅包含User-Agent
22+
headers = {
23+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
24+
}
25+
26+
# 发送GET请求获取登录页面
27+
response = session.get('https://www.51daili.com', headers=headers, timeout=10)
28+
response.raise_for_status()
29+
30+
# 从响应头中获取PHPSESSID
31+
phpsessid = None
32+
if 'set-cookie' in response.headers:
33+
cookies = response.headers['set-cookie'].split(';')
34+
for cookie in cookies:
35+
if 'PHPSESSID' in cookie:
36+
phpsessid = cookie.split('=')[1]
37+
break
38+
39+
# 解析HTML获取令牌
40+
soup = BeautifulSoup(response.text, 'html.parser')
41+
token_input = soup.find('input', {'name': '__login_token__'})
42+
43+
if token_input:
44+
login_token = token_input.get('value')
45+
print(f"成功获取登录令牌: {login_token}")
46+
else:
47+
print("未找到登录令牌,使用默认令牌")
48+
login_token = "14f2877f31842495ff24e3d73036158a"
49+
50+
if phpsessid:
51+
print(f"成功获取PHPSESSID: {phpsessid}")
52+
else:
53+
print("未能从响应头中获取PHPSESSID,使用默认值")
54+
phpsessid = "qkheban0ocfthart1bk7s861kn"
55+
56+
# 准备登录数据和更新headers
57+
login_data = {
58+
'__login_token__': login_token,
59+
'account': username,
60+
'password': password,
61+
'ticket': '1',
62+
'keeplogin': '1',
63+
'is_read': '1'
64+
}
65+
66+
# 更新headers,添加Cookie
67+
headers['Cookie'] = f"PHPSESSID={phpsessid};tncode_check=ok"
68+
69+
print("正在尝试登录...")
70+
time.sleep(1) # 添加短暂延迟
71+
72+
# 发送POST请求进行登录
73+
login_url = 'https://www.51daili.com/index/user/login.html'
74+
login_response = session.post(login_url, data=login_data, headers=headers, timeout=10)
75+
login_response.raise_for_status()
76+
77+
# 检查登录是否成功
78+
if login_response.status_code == 200:
79+
print("登录请求已发送,状态码: 200")
80+
81+
# 尝试从响应头中查找token
82+
print("响应头:", login_response.headers)
83+
84+
token = None
85+
# 检查Set-Cookie头
86+
if 'set-cookie' in login_response.headers:
87+
cookies = login_response.headers['set-cookie'].split(';')
88+
for cookie in cookies:
89+
if 'token' in cookie.lower():
90+
print(f"找到token cookie: {cookie}")
91+
# 提取token值
92+
token_parts = cookie.split('=')
93+
if len(token_parts) >= 2:
94+
token = token_parts[1].strip()
95+
break
96+
97+
# 如果没有在Set-Cookie中找到,检查其他头字段
98+
if not token:
99+
for key, value in login_response.headers.items():
100+
if 'token' in key.lower():
101+
print(f"找到token头: {key}: {value}")
102+
token = value
103+
break
104+
105+
if token:
106+
print(f"成功获取token: {token}")
107+
108+
# 使用token请求签到页面
109+
signin_headers = headers.copy()
110+
# 添加token到Cookie
111+
signin_headers['Cookie'] = f"{signin_headers.get('Cookie', '')}; token={token}"
112+
signin_headers['Referer'] = 'https://www.51daili.com/'
113+
114+
print("正在请求签到页面...")
115+
signin_response = session.get(
116+
'https://www.51daili.com/index/user/signin.html',
117+
headers=signin_headers,
118+
timeout=10
119+
)
120+
signin_response.raise_for_status()
121+
122+
print("签到页面响应内容:")
123+
print(signin_response.text)
124+
else:
125+
print("未能从登录响应中提取token")
126+
else:
127+
print(f"登录请求返回异常状态码: {login_response.status_code}")
128+
129+
return session
130+
131+
except requests.exceptions.RequestException as e:
132+
print(f"请求过程中发生错误: {e}")
133+
return None
134+
135+
if __name__ == "__main__":
136+
print("51代理登录示例")
137+
print("=" * 30)
138+
139+
session = login_to_51daili()
140+
141+
if session:
142+
print("登录流程完成")
143+
# 这里可以继续使用session进行后续操作
144+
else:
145+
print("登录失败")

0 commit comments

Comments
 (0)