66
77import re
88import rsa
9- import ssl
109import time
1110import json
1211import base64
1312import logging
1413import binascii
14+ import requests
1515import urllib .parse
1616
17- # 参考PSpider项目
18- import spider
19- ssl ._create_default_https_context = ssl ._create_unverified_context
20-
2117
2218class WeiBoLogin (object ):
2319 """
@@ -28,51 +24,34 @@ def __init__(self):
2824 """
2925 constructor
3026 """
31- self .user_name = None # 登录用户名
32- self .pass_word = None # 登录密码
33- self .user_uniqueid = None # 用户唯一ID
34- self .user_nick = None # 用户昵称
27+ self .user_name = None
28+ self .pass_word = None
29+ self .user_uniqueid = None
30+ self .user_nick = None
3531
36- self .cookie_jar , self .opener = None , None
32+ self .session = requests .Session ()
33+ self .session .headers .update ({"User-Agent" : "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:41.0) Gecko/20100101 Firefox/41.0" })
34+ self .session .get ("http://weibo.com/login.php" )
3735 return
3836
39- def login (self , user_name , pass_word , proxies = None ):
37+ def login (self , user_name , pass_word ):
4038 """
4139 login weibo.com, return True or False
4240 """
43- # 变量赋值初始化
4441 self .user_name = user_name
4542 self .pass_word = pass_word
4643 self .user_uniqueid = None
4744 self .user_nick = None
4845
49- # 构建cookie_jar和opener,这里不使用代理,同时保证整个流程中不需要关心cookie问题
50- self .cookie_jar , self .opener = spider .make_cookiejar_opener (is_cookie = True , proxies = proxies )
51- self .opener .addheaders = spider .make_headers (
52- user_agent = "pc" ,
53- host = "weibo.com" ,
54- referer = "http://weibo.com/" ,
55- accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" ,
56- accept_encoding = "gzip, deflate" ,
57- accept_language = "zh-CN,zh;q=0.8"
58- ).items ()
59-
60- # (1) 打开weibo.com/login.php,先请求一些必要的cookie信息
61- self .opener .open ("http://weibo.com/login.php" )
62-
63- # (2) 根据用户名获取加密后的用户名
46+ # get json data
6447 s_user_name = self .get_username ()
65-
66- # (3) 利用加密后的用户名,获取其他一些数据:json格式
6748 json_data = self .get_json_data (su_value = s_user_name )
6849 if not json_data :
6950 return False
70-
71- # (4) 根据第三步得到的json数据,获取加密后的密码
7251 s_pass_word = self .get_password (json_data ["servertime" ], json_data ["nonce" ], json_data ["pubkey" ])
7352
74- # (5) 构造登录中用到的postdata
75- post_dict = {
53+ # make post_data
54+ post_data = {
7655 "entry" : "weibo" ,
7756 "gateway" : "1" ,
7857 "from" : "" ,
@@ -93,35 +72,28 @@ def login(self, user_name, pass_word, proxies=None):
9372 "returntype" : "TEXT" ,
9473 }
9574
96- # (6) 判断是否需要输入验证码,如果需要,获取验证码并进行打码操作
97- if json_data . get ( "showpin" , None ) == 1 :
75+ # get captcha code
76+ if json_data [ "showpin" ] == 1 :
9877 url = "http://login.sina.com.cn/cgi/pin.php?r=%d&s=0&p=%s" % (int (time .time ()), json_data ["pcid" ])
9978 with open ("captcha.jpeg" , "wb" ) as file_out :
100- file_out .write (self .opener . open (url ).read () )
79+ file_out .write (self .session . get (url ).content )
10180 code = input ("请输入验证码:" )
102- # cid, code = self.yundama.get_captcha(self.opener.open(url).read(), "captcha.jpeg", "image/jpeg", codetype="1005")
103- # if not code:
104- # return False
105- post_dict ["pcid" ] = json_data ["pcid" ]
106- post_dict ["door" ] = code
81+ post_data ["pcid" ] = json_data ["pcid" ]
82+ post_data ["door" ] = code
10783
108- # (7) 根据构造的postdata,登录微博
84+ # login weibo.com
10985 login_url_1 = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)&_=%d" % int (time .time ())
110- json_data_1 = json . loads ( spider . get_html_content ( self .opener . open (login_url_1 , data = spider . make_post_data ( post_dict ))) )
86+ json_data_1 = self .session . post (login_url_1 , data = post_data ). json ( )
11187 if json_data_1 ["retcode" ] == "0" :
112- # 登录后有一个跳转, 构造跳转链接的postdata
113- post_dict = {
88+ params = {
11489 "callback" : "sinaSSOController.callbackLoginStatus" ,
90+ "client" : "ssologin.js(v1.4.18)" ,
11591 "ticket" : json_data_1 ["ticket" ],
11692 "ssosavestate" : int (time .time ()),
117- "client" : "ssologin.js(v1.4.18)" ,
11893 "_" : int (time .time ()* 1000 ),
11994 }
120- login_url_2 = "https://passport.weibo.com/wbsso/login?" + urllib .parse .urlencode (post_dict )
121- html_data = spider .get_html_content (self .opener .open (login_url_2 ), charset = "gbk" )
122- json_data_2 = json .loads (re .search ("\((?P<result>.*)\)" , html_data ).group ("result" ))
123-
124- # 检查登录是否成功,并获取用户唯一ID,用户昵称等
95+ response = self .session .get ("https://passport.weibo.com/wbsso/login" , params = params )
96+ json_data_2 = json .loads (re .search (r"\((?P<result>.*)\)" , response .text ).group ("result" ))
12597 if json_data_2 ["result" ] is True :
12698 self .user_uniqueid = json_data_2 ["userinfo" ]["uniqueid" ]
12799 self .user_nick = json_data_2 ["userinfo" ]["displayname" ]
@@ -134,7 +106,7 @@ def login(self, user_name, pass_word, proxies=None):
134106
135107 def get_username (self ):
136108 """
137- get username, encrypt file: http://tjs.sjs.sinajs.cn/t5/register/js/page/remote/loginLayer.js
109+ get legal username
138110 """
139111 username_quote = urllib .parse .quote_plus (self .user_name )
140112 username_base64 = base64 .b64encode (username_quote .encode ("utf-8" ))
@@ -144,20 +116,18 @@ def get_json_data(self, su_value):
144116 """
145117 get the value of "servertime", "nonce", "pubkey", "rsakv" and "showpin", etc
146118 """
147- post_data = urllib . parse . urlencode ( {
119+ params = {
148120 "entry" : "weibo" ,
149121 "callback" : "sinaSSOController.preloginCallBack" ,
150122 "rsakt" : "mod" ,
151123 "checkpin" : "1" ,
152124 "client" : "ssologin.js(v1.4.18)" ,
153125 "su" : su_value ,
154126 "_" : int (time .time ()* 1000 ),
155- })
156-
127+ }
157128 try :
158- response = self .opener .open ('http://login.sina.com.cn/sso/prelogin.php?' + post_data )
159- data = spider .get_html_content (response , charset = "utf-8" )
160- json_data = json .loads (re .search ("\((?P<data>.*)\)" , data ).group ("data" ))
129+ response = self .session .get ("http://login.sina.com.cn/sso/prelogin.php" , params = params )
130+ json_data = json .loads (re .search (r"\((?P<data>.*)\)" , response .text ).group ("data" ))
161131 except Exception as excep :
162132 json_data = {}
163133 logging .error ("WeiBoLogin get_json_data error: %s" , excep )
@@ -167,17 +137,16 @@ def get_json_data(self, su_value):
167137
168138 def get_password (self , servertime , nonce , pubkey ):
169139 """
170- get legal password, encrypt file: http://tjs.sjs.sinajs.cn/t5/register/js/page/remote/loginLayer.js
140+ get legal password
171141 """
172- string = (str (servertime ) + ' \t ' + str (nonce ) + ' \n ' + str (self .pass_word )).encode ("utf-8" )
142+ string = (str (servertime ) + " \t " + str (nonce ) + " \n " + str (self .pass_word )).encode ("utf-8" )
173143 public_key = rsa .PublicKey (int (pubkey , 16 ), int ("10001" , 16 ))
174144 password = rsa .encrypt (string , public_key )
175145 password = binascii .b2a_hex (password )
176146 return password .decode ()
177147
178148
179- if __name__ == ' __main__' :
149+ if __name__ == " __main__" :
180150 logging .basicConfig (level = logging .DEBUG , format = "%(asctime)s\t %(levelname)s\t %(message)s" )
181- # 测试登录,输入微博的用户名和密码
182151 weibo = WeiBoLogin ()
183152 weibo .login ("username" , "password" )
0 commit comments