|
@@ -0,0 +1,157 @@
|
|
1
|
+import requests,json,random,execjs,uuid
|
|
2
|
+from urllib import parse
|
|
3
|
+from urllib.parse import quote
|
|
4
|
+class WebDouYin:
|
|
5
|
+ def __init__(self):
|
|
6
|
+ self.proxies = {
|
|
7
|
+ }
|
|
8
|
+ def get_signature(self,url=None,method='_signature'):
|
|
9
|
+ with open('signature.js', 'r', encoding='utf-8') as f:
|
|
10
|
+ b = f.read()
|
|
11
|
+ c = execjs.compile(b)
|
|
12
|
+ # url=url.replace('%28','(').replace('%29',')').replace('%2C',',')
|
|
13
|
+ d = c.call(method, url.replace('\n',''))
|
|
14
|
+ print('_signature',d)
|
|
15
|
+ return d
|
|
16
|
+ def get_ck(self):
|
|
17
|
+ headers = {
|
|
18
|
+ 'authority': 'www.douyin.com',
|
|
19
|
+ 'method': 'GET',
|
|
20
|
+ 'path': '/',
|
|
21
|
+ 'scheme': 'https',
|
|
22
|
+ 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
|
|
23
|
+ 'accept-encoding': 'gzip, deflate, br',
|
|
24
|
+ 'accept-language': 'zh-CN,zh;q=0.9',
|
|
25
|
+ 'upgrade-insecure-requests': '1',
|
|
26
|
+ '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',
|
|
27
|
+ }
|
|
28
|
+ res = requests.get('https://www.douyin.com/', headers=headers,verify=False)
|
|
29
|
+ cookie_dict=res.cookies.get_dict()
|
|
30
|
+ ck=''
|
|
31
|
+ if cookie_dict:
|
|
32
|
+ for k,v in cookie_dict.items():
|
|
33
|
+ ck+='%s=%s; '%(k,v)
|
|
34
|
+ ck=ck[:-2]
|
|
35
|
+ ck+='; passport_csrf_token='+str(uuid.uuid4()).replace('-','')
|
|
36
|
+ print(ck)
|
|
37
|
+ return ck
|
|
38
|
+ def get_ua_ck(self,type_name=None):
|
|
39
|
+ ua_list=[
|
|
40
|
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
|
|
41
|
+ ]
|
|
42
|
+ ck_list=[
|
|
43
|
+ 'ttwid=1%7CTVzdM0P0u-8dtsmh6c-EaQEtBoTSOs_MG85FAg07AbA%7C1631502013%7C66442d8594de8e93ad18b73f3dfe0c94ed864c3d932824bcde9918b5be172321; passport_csrf_token=866923f1a32045fd82e47053158402a2'
|
|
44
|
+ ]
|
|
45
|
+ return random.choice(ua_list),random.choice(ck_list)
|
|
46
|
+ def response(self,url,headers,proxy,data=None):
|
|
47
|
+ try:
|
|
48
|
+ if data:
|
|
49
|
+ res = requests.post(url, headers=headers,verify=False, proxies=proxy,data=data, timeout=8)
|
|
50
|
+ else:
|
|
51
|
+ res=requests.get(url,headers=headers,verify=False,proxies=proxy,timeout=8)
|
|
52
|
+ print(res.content.decode())
|
|
53
|
+ json_data=json.loads(res.content.decode())
|
|
54
|
+ except Exception as e:
|
|
55
|
+ json_data = "请求失"
|
|
56
|
+ return json_data
|
|
57
|
+ def get_user_videos(self,sec_user_id,max_cursor=0,count=10,proxy=None):
|
|
58
|
+ ua,ck=self.get_ua_ck('get_user_videos')
|
|
59
|
+ url='https://www.douyin.com/aweme/v1/web/aweme/post/?'
|
|
60
|
+ param={
|
|
61
|
+ 'device_platform': 'webapp',
|
|
62
|
+ 'aid': '6383',
|
|
63
|
+ 'channel': 'channel_pc_web',
|
|
64
|
+ 'sec_user_id': sec_user_id,
|
|
65
|
+ 'max_cursor': str(max_cursor),
|
|
66
|
+ 'count': str(count),
|
|
67
|
+ 'publish_video_strategy_type': '2',
|
|
68
|
+ 'version_code': '160100',
|
|
69
|
+ 'version_name': '16.1.0',
|
|
70
|
+ 'cookie_enabled': 'true',
|
|
71
|
+ 'screen_width': '1920',
|
|
72
|
+ 'screen_height': '1080',
|
|
73
|
+ 'browser_language': 'zh-CN',
|
|
74
|
+ 'browser_platform': 'Win32',
|
|
75
|
+ 'browser_name': 'Mozilla',
|
|
76
|
+ 'browser_version':ua.replace('Mozilla/',''),
|
|
77
|
+ 'browser_online': 'true',
|
|
78
|
+ }
|
|
79
|
+
|
|
80
|
+ url = url + parse.urlencode(param)
|
|
81
|
+ _signature = self.get_signature(url)
|
|
82
|
+ url+='&_signature='+quote(_signature)
|
|
83
|
+
|
|
84
|
+ headers = {
|
|
85
|
+ "authority": "www.douyin.com",
|
|
86
|
+ "method": "GET",
|
|
87
|
+ "path": str(url).replace('https://www.douyin.com',''),
|
|
88
|
+ "scheme": "https",
|
|
89
|
+ "accept": "application/json, text/plain, */*",
|
|
90
|
+ # "accept-encoding": "gzip, deflate, br",
|
|
91
|
+ "accept-language": "zh-CN,zh;q=0.9",
|
|
92
|
+ "cookie": ck,
|
|
93
|
+ "referer": "https://www.douyin.com/user/{sec_user_id}?enter_method=search_result&enter_from=search_result".format(sec_user_id=sec_user_id),
|
|
94
|
+ "user-agent":ua,
|
|
95
|
+ "withcredentials": "true",
|
|
96
|
+ }
|
|
97
|
+ if ck:headers['cookie']=ck
|
|
98
|
+ json_data=self.response(url=url,headers=headers,proxy=proxy)
|
|
99
|
+ return json_data
|
|
100
|
+ # print(json_data)
|
|
101
|
+ def get_user_info(self,sec_user_id,proxy=None):
|
|
102
|
+ ua,ck=self.get_ua_ck()
|
|
103
|
+ url='https://www.douyin.com/aweme/v1/web/user/profile/other/?'
|
|
104
|
+ param={
|
|
105
|
+ 'device_platform': 'webapp',
|
|
106
|
+ 'aid': '6383',
|
|
107
|
+ 'channel': 'channel_pc_web',
|
|
108
|
+ 'publish_video_strategy_type': '2',
|
|
109
|
+ 'source': 'channel_pc_web',
|
|
110
|
+ 'sec_user_id': sec_user_id,
|
|
111
|
+ 'version_code': '160100',
|
|
112
|
+ 'version_name': '16.1.0',
|
|
113
|
+ 'cookie_enabled': 'true',
|
|
114
|
+ 'screen_width': '1920',
|
|
115
|
+ 'screen_height': '1080',
|
|
116
|
+ 'browser_language': 'zh-CN',
|
|
117
|
+ 'browser_platform': 'Win32',
|
|
118
|
+ 'browser_name': 'Mozilla',
|
|
119
|
+ 'browser_version':ua.replace('Mozilla/',''),
|
|
120
|
+ 'browser_online': 'true',
|
|
121
|
+ }
|
|
122
|
+ url = url + parse.urlencode(param)
|
|
123
|
+ _signature = self.get_signature(url)
|
|
124
|
+ url+='&_signature='+quote(_signature)
|
|
125
|
+
|
|
126
|
+ headers = {
|
|
127
|
+ "authority": "www.douyin.com",
|
|
128
|
+ "method": "GET",
|
|
129
|
+ "path": str(url).replace('https://www.douyin.com',''),
|
|
130
|
+ "scheme": "https",
|
|
131
|
+ "accept": "application/json, text/plain, */*",
|
|
132
|
+ "accept-language": "zh-CN,zh;q=0.9",
|
|
133
|
+ "cookie": ck,
|
|
134
|
+ "referer": "https://www.douyin.com/user/{sec_user_id}?enter_method=search_result&enter_from=search_result".format(sec_user_id=sec_user_id),
|
|
135
|
+ "user-agent":ua,
|
|
136
|
+ "withcredentials": "true",
|
|
137
|
+ }
|
|
138
|
+ if ck:headers['cookie']=ck
|
|
139
|
+ json_data=self.response(url=url,headers=headers,proxy=proxy)
|
|
140
|
+ return json_data
|
|
141
|
+if __name__ == '__main__':
|
|
142
|
+ webdy=WebDouYin()
|
|
143
|
+ res = webdy.get_user_videos('MS4wLjABAAAAqLPgx-hHf27EqGEtRQ6YyuQQTmikB5CBO1jXy61yhWKujGd8KO5G8V2vdcLQJAym')
|
|
144
|
+ print(res)
|
|
145
|
+ # webdy.get_user_info('MS4wLjABAAAAC2euvL-0qMZyd80aNwZa-wX5KXuz_r7YVNHSBOogfVg')
|
|
146
|
+ # webdy.get_ck()
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|