1
+ import requests
2
+ import random
3
+ import csv
4
+ import time
5
+
6
+ headers = {
7
+ "User-Agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36" ,
8
+ }
9
+
10
+ def get_tid ():
11
+ """
12
+ 获取tid,c,w
13
+ :return:tid
14
+ """
15
+ tid_url = "https://passport.weibo.com/visitor/genvisitor"
16
+ data = {
17
+ "cb" : "gen_callback" ,
18
+ "fp" : {
19
+ "os" : "3" ,
20
+ "browser" : "Chrome69,0,3497,100" ,
21
+ "fonts" : "undefined" ,
22
+ "screenInfo" : "1920*1080*24" ,
23
+ "plugins" : "Portable Document Format::internal-pdf-viewer::Chrome PDF Plugin|::mhjfbmdgcfjbbpaeojofohoefgiehjai::Chrome PDF Viewer|::internal-nacl-plugin::Native Client"
24
+ }
25
+ }
26
+ req = requests .post (url = tid_url , data = data , headers = headers )
27
+
28
+ if req .status_code == 200 :
29
+ ret = eval (req .text .replace ("window.gen_callback && gen_callback(" , "" ).replace (");" , "" ).replace ("true" , "1" ))
30
+ return ret .get ('data' ).get ('tid' )
31
+ return None
32
+
33
+
34
+ def get_cookie ():
35
+ """
36
+ 获取完整的cookie
37
+ :return: cookie
38
+ """
39
+ tid = get_tid ()
40
+ if not tid :
41
+ return None
42
+
43
+ cookies = {
44
+ "tid" : tid + "__095" # + tid_c_w[1]
45
+ }
46
+ url = "https://passport.weibo.com/visitor/visitor?a=incarnate&t={tid}" \
47
+ "&w=2&c=095&gc=&cb=cross_domain&from=weibo&_rand={rand}"
48
+ req = requests .get (url .format (tid = tid , rand = random .random ()),
49
+ cookies = cookies , headers = headers )
50
+ if req .status_code != 200 :
51
+ return None
52
+
53
+ ret = eval (req .text .replace ("window.cross_domain && cross_domain(" , "" ).replace (");" , "" ).replace ("null" , "1" ))
54
+
55
+ try :
56
+ sub = ret ['data' ]['sub' ]
57
+ if sub == 1 :
58
+ return None
59
+ subp = ret ['data' ]['subp' ]
60
+ except KeyError :
61
+ return None
62
+ return sub , subp
63
+
64
+ if __name__ == "__main__" :
65
+ i = 0
66
+ while True :
67
+ try :
68
+ sub , subp = get_cookie ()
69
+ print (sub )
70
+ print (subp )
71
+ i += 1
72
+ with open ('cookies.csv' ,'a' ,newline = '' ,encoding = 'utf-8' ) as f :
73
+ writer = csv .writer (f )
74
+ L = [i ,sub ,subp ]
75
+ # print(L)
76
+ writer .writerow (L )
77
+ print ("\n =====" )
78
+ time .sleep (10 )
79
+ except :
80
+ time .sleep (0.1 )
0 commit comments