-
Notifications
You must be signed in to change notification settings - Fork 0
/
tieba_web.py
130 lines (114 loc) · 3.48 KB
/
tieba_web.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import re
import os
import sys
import time
import urllib.parse
from requests_html import HTMLSession
session = HTMLSession()
def likes_200():
resp = session.get('https://tieba.baidu.com/mo/q/newmoindex', headers=header)
if resp.status_code == 200:
data = resp.json()
likes = data['data']['like_forum']
likes_filter = list(filter(lambda x: x['is_sign'] == 0, likes))
return [x['forum_name'] for x in likes_filter]
return []
def likes_last():
t = int(round(time.time() * 1000))
url = 'http://tieba.baidu.com/f/like/mylike'
resp = session.get(url, params={'v': t}, headers=header)
if resp.status_code == 200:
_as = resp.html.xpath('//*[@id="j_pagebar"]/div/a/@href')
last_page = int(_as[-1].split('=')[1])
likes = []
if last_page > 10:
for i in range(11, last_page + 1):
params = {
'v': t,
'pn': i
}
resp = session.get(url, params=params, headers=header)
if resp.status_code == 200:
spans = resp.html.xpath('/html/body/div[1]/div[1]/table/tr/td/span')
for x in spans:
name = urllib.parse.unquote(x.attrs['balvname'], encoding='gbk')
likes.append(name)
return likes
def all_likes():
likes = likes_200()
last_likes = likes_last()
likes.extend(last_likes)
return likes
def get_tbs(kw):
"""
Args:
kw: 吧名
Returns: tbs是每一个页面的随机值
"""
url = f'https://tieba.baidu.com/f?kw={kw}'
resp = session.get(url, headers=header)
re_result = re.findall('tbs(.*)};', resp.text)
regex = r'"(.*)"'
tbs = re.findall(regex, re_result[0])[0]
return tbs
def sign(kw, tbs):
url = 'https://tieba.baidu.com/sign/add'
data = {
'ie': 'utf-8',
'kw': kw,
'tbs': tbs
}
resp = session.post(url, data=data, headers=header)
if resp.status_code == 200:
data = resp.json()
if data['error']:
return False
if data['data'].get('errmsg', '') == 'success':
return True
return False
def get_header():
stoken = os.getenv('STOKEN')
bduss = os.getenv('BDUSS')
header = {
"Accept": "application/json",
"Content-Type": "application/json",
"Host": "tieba.baidu.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36",
"Cookie": f"BDUSS={bduss}; STOKEN={stoken}"
}
return header
if __name__ == '__main__':
header = get_header()
try:
kws = all_likes()
total = len(kws)
print(f'获取到{total}个吧')
except Exception as e:
print(e.__repr__())
print('获取贴吧列表失败')
sys.exit()
sign_failure = []
n = 0
for kw in kws:
n += 1
if (n % 6) == 0:
time.sleep(0.05)
try:
tbs = get_tbs(kw)
flag = sign(kw, tbs)
if flag:
print(kw)
except Exception as e:
sign_failure.append(kw)
continue
for kw in sign_failure:
try:
tbs = get_tbs(kw)
flag = sign(kw, tbs)
if flag:
print(kw)
continue
print(f'签到失败: {kw}')
except Exception as e:
print(e.__repr__())
continue