One of Sina Weibo data mining recipes: Login (API)

Source: Internet
Author: User
Tags urlencode truncated

#!/usr/bin/python #-*-Coding:utf-8-*-"Created on 2014-12-28@author:beyondzhou@name:login.py" ' Import Re, jsonimpo RT Urllib, Urllib2, URLLIB3, Cookielibimport base64, RSA, BINASCII # encryptfrom Weibo import Apiclientclass smartredirect Handler (URLLIB2. Httpredirecthandler): Def http_error_301 (self, CLS, req, FP, code, MSG, headers): result = Urllib2. httpredirecthandler.http_error_301 (CLS, req, FP, code, MSG, headers) Result.status = code return result D EF http_error_302 (self, CLS, req, FP, code, MSG, headers): result = Urllib2. httpredirecthandler.http_error_302 (CLS, req, FP, code, MSG, headers) Result.status = code return result D EF Get_cookie (): cookies = cookielib. Cookiejar () return URLLIB2. Httpcookieprocessor (Cookies) def get_opener (proxy=false): Rv=urllib2.build_opener (Get_cookie (), Smartredirecthandler ()) rv.addheaders = [(' User-agent ', ' mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)] Return RVCLASS Sinaapi (): Def __init__ (self, callback_url, App_key, Redirect_url, user_id, user_pswd): self. Callback_url = Callback_url self. App_key = App_key self. Redirect_url = Redirect_url self. user_id = user_id self. USER_PSWD = user_pswd Self.http = urllib3. Poolmanager () def get_username (self, user_id): # The encryption algorithm of username # Ssologin        . Js:ah.su=sinassoencoder.base64.encode (AJ); User_id_ = Urllib.quote (user_id) # encode username, avoid error refer:@ & su = base64.encodestring (user_id_) [ : -1] Return su def get_password_rsa (self, user_pswd, PubKey, Servertime, nonce): # RSA Encrypt: #whe n pwencode = "RSA2" rsapubkey = Int (PubKey, 6553) #pubkey from-to-key_1 = Int (' 10001 ', +) #10001 to 7 key = RSA.  PublicKey (Rsapubkey, key_1) # message = str (servertime) + "\ T" + str (nonce) + "\ n" + str (user_pswd) passwd = Rsa.encrypt (MessagE, key) passwd = Binascii.b2a_hex (passwd) #to return passwd def get_parameter (self): su = Self.get_username (self. user_id) url = "https://login.sina.com.cn/sso/prelogin.php?entry=openapi&callback=        sinassocontroller.prelogincallback&su= "+su+" &rsakt=mod&checkpin=1&client=ssologin.js (v1.4.15) " r = self.http.request (' GET ', url) p = re.compile (' \ ((. *) \) ') Json_data = P.search (r.data). Group (1) DA Ta = json.loads (json_data) PubKey = data[' PubKey '] pcid = data[' pcid '] servertime = str (data[ ' Servertime ']) nonce = data[' nonce '] rsakv = str (data[' rsakv ']) SP = Self.get_password_rsa (self. USER_PSWD, PubKey, Servertime, nonce) #print pcid; Print servertime; Print nonce; Print rsakv; Print SP; Print Su return pcid, Servertime, nonce, rsakv, SP, su def get_ticket (self): pcid, Servertime, n Once, rsakv, sp, su = Self.get_parameter() fields = Urllib.urlencode ({' entry ': ' Openapi ', ' Gateway ': ' 1 ', '            From ': ', ' savestate ': ' 0 ', ' useticket ': ' 1 ', ' pagerefer ': ',         ' Pcid ': pcid, ' ct ': ' 1800 ', ' s ': ' 1 ', ' VSNF ' : ' 1 ', ' vsnval ': ', ' door ': ', ' appkey ': ' kxr5r ', '        Su ': su, ' service ': ' Miniblog ', ' servertime ': servertime, ' nonce '            : nonce, ' pwencode ': ' rsa2 ', ' rsakv ': rsakv, ' SP ': SP, ' SR ': ' 1680*1050 ', ' Encoding ': ' UTF-8 ', ' cdult ': ' 2 ', ' Domai                   N ': ' weibo.com ', ' prelt ': ' 0 ', ' returntype ': ' TEXT ',} ' headers = { # "RequEST ":" Post/sso/login.php?client=ssologin.js (v1.4.15) &_=1400652171542 http/1.1 ", #" Accept ":" */* ", "Content-type": "Application/x-www-form-urlencoded", # "Referer": Self.                   Callback_url, # "Accept-language": "ZH-CN", # "Origin": "Https://api.weibo.com", # "accept-encoding": "gzip, deflate", # "User-agent": "mozilla/5.0 (Windows NT 6.1; WOW64; trident/7.0; EIE10; Zhcnmse;                   rv:11.0) Like Gecko ", #" Host ":" login.sina.com.cn ", #" Connection ":" Keep-alive ", # "Cache-control": "No-cache", "url =" Https://login.sina.com.cn/sso/login.php?clien T=ssologin.js (v1.4.15) "req = Urllib2. Request (URL, fields, headers) F = urllib2.urlopen (req) data = Json.loads (F.read ()) return data["Ticke T "] def get_code_security (self): ticket = Self.get_ticket () fields = uRllib.urlencode ({' Action ': ' Submit ', # must ' Display ': ' Default ', ' Withofficalflag ': ' 0 ' , # must ' Quick_auth ': ' null ', ' withofficalaccount ': ', ' scope ': ', ' ticket ' : Ticket, # must ' Isloginsina ': ', ' response_type ': ' Code ', # must ' Regcallback ': ' Htt Ps://api.weibo.com/2/oauth2/authorize?client_id= ' +self. app_key+ ' &response_type=code&display=default&redirect_uri= ' +self. redirect_url+ ' &from=&with_cookie= ', ' Redirect_uri ': self. Redirect_url, # must ' client_id ': self.            App_key, # must ' appkey62 ': ' kxr5r ', ' state ': ', # must ' Verifytoken ': ' null ', ' From ': ', # must ' userId ': ' ", # does not need enter UserId ' passwd ':" ", # does not need enter pass Word}) Login_url = ' https://api.weibo.com/oauth2/authorize ' headers = {"User-agent": "MOZILLA/5 .0 (Windows NT 6.1; WOW64; trident/7.0; EIE10; Zhcnmse; rv:11.0) Like Gecko "," Referer ": Self. Callback_url, "Content-type": "Application/x-www-form-urlencoded",} req = Urll Ib2.                Request (Login_url, fields, headers) req_ =urllib2.urlopen (req) Return_redirect_uri = Req_.geturl () print ' Return_redirect_uri: ', Return_redirect_uri code = Re.findall (? <=code%3d). { 32}| (? <=code=). {32}| (? <=code%253d). {+} ", Return_redirect_uri) # URL is formatted with%3d or= return code def Get_code_ns (self): Fiel ds = Urllib.urlencode ({' Action ': ' Submit ', # must ' Display ': ' Default ', ' Withofficalflag ': ' 0 ', # must ' Quick_auth ': ' null ', ' withofficalaccount ': ', ' scope ': ', ' t Icket ': ', # must ' Isloginsina ': ', ' response_type ': ' Code ', # must ' Regcallback ': ' , ' redirect_uri ': Self. Redirect_url, # must ' client_id ': self.            App_key, # must ' appkey62 ': ' kxr5r ', ' state ': ', # must ' Verifytoken ': ' null ', ' From ': ', # must ' UserId ': self. USER_ID, # must ' passwd ': self. USER_PSWD, # must}) Login_url = ' https://api.weibo.com/oauth2/authorize ' headers = {"User-agent ":" mozilla/5.0 (Windows NT 6.1; WOW64; trident/7.0; EIE10; Zhcnmse; rv:11.0) Like Gecko "," Referer ": Self. Callback_url, "Content-type": "Application/x-www-form-urlencoded",} r = Urllib               2.Request (Login_url, fields, headers) opener = Get_opener (False) Urllib2.install_opener (opener) Try:                     F = Opener.open (r) Return_redirect_uri = f.url print "NS1", Return_redirect_uri Except Urllib2. Httperror, E:return_redirect_uri = E.geturl () print "NS2 ", Return_redirect_uri # get the code #code = return_redirect_uri.split (' = ') [1] # Re-generate Wit h regexp Expression Code = Re.findall (r "(? <=code%3d). { 32}| (? <=code=). {+} ", Return_redirect_uri) print code return Code def weibo_login (): # Sina Weibo basic secret Informa tion App_key = U ' # app KEY app_secret = U ' # app SECRET redirect_url = ' user_name = ' user_passwd = ' ' Client = apiclient (App_key=app_key, App_secret=app_secret, redirect_uri=redirect_url) Callback_url = client.ge T_authorize_url () print ' Callback_url: ', Callback_url API = Sinaapi (Callback_url, App_key, Redirect_url, USER_NAME,      USER_PASSWD) code = api.get_code_security () print ' Code: ', Code requests = Client.request_access_token (code) Access_token = Requests.access_token expires_in = requests.expires_in # Access_token client.set_access _token (Access_token, expires_in) return clientif __name__ = = ' __main__ ': # get Weibo_api to access Sina API Weibo_api = weibo_login () print ' Weibo_api: ', Weibo_api # Get public Weibo statuses = Weibo_api.statuses.public_timeline.get (count=200) print json.dumps (statuses, indent=  1)

Result:

Code: [' 514b806e619c320b7b1ed85ec0d9880a ']weibo_api: <weibo. Apiclient object at 0x027786d0>{"interval": 0, "hasvisible": false, "Total_number": 2, "Previous_cursor": 0, "next _cursor ": 0," statuses ": [{" Reposts_count ": 0," truncated ": false," text ":" \u7238\u7238\uff0c\u5c31\u50cf\u8f d9\u4e48\u75bc\u2026\u2026 "," visible ": {" type ": 0," list_id ": 0}," in_reply_to_status_id ":" "," Bmidd Le_pic ":" Http://ww1.sinaimg.cn/bmiddle/dea230dbjw1enp0tkfoy7j20cs281dm7.jpg "," id ": 3792648903463407," Thumbnail_ Pic ":" Http://ww1.sinaimg.cn/thumbnail/dea230dbjw1enp0tkfoy7j20cs281dm7.jpg "," Mid ":" 3792648903463407 "," source ": "<a href=\" http://app.weibo.com/t/feed/3auc5p\ "rel=\" nofollow\ ">\u76ae\u76ae\u65f6\u5149\u673a</a>" , "Attitudes_count": 0, "in_reply_to_screen_name": "", "Pic_urls": [{"Thumbnail_pic": "Http://ww1.sinaim    G.cn/thumbnail/dea230dbjw1enp0tkfoy7j20cs281dm7.jpg "}]," in_reply_to_user_id ":" ","Darwin_tags": [], "favorited": false, "original_pic": "HTTP://WW1.SINAIMG.CN/LARGE/DEA230DBJW1ENP0TKFOY7J20CS281DM     7.jpg "," Idstr ":" 3792648903463407 "," Source_type ": 1," user ": {" Bi_followers_count ": 310," Domain ":" ", "Avatar_large": "http://tp4.sinaimg.cn/3735171291/180/40031850543/0", "Verified_source": "", "PType": 0, " Statuses_count ": 1589," allow_all_comment ": true," id ": 3735171291," Verified_reason_url ":" "," City ":" 10     XX "," Province ":" Credit_score "," Block_app ": 0," Follow_me ": false," Verified_reason ":" ", "Followers_count": 563, "location": "\u91cd\u5e86", "Verified_trade": "", "Mbtype": 0, "Verified_sourc E_url ":", "Profile_url": "u/3735171291", "Block_word": 0, "AVATAR_HD": "http://tp4.sinaimg.cn/3735171291/180     /40031850543/0 "," star ": 0," description ":" "," Friends_count ": 772," Online_status ": 0," Mbrank ": 0, "Idstr": "3735171291", "Profile_image_url": "http://tp4.sinaimg.cn/3735171291/50/40031850543/0", "allow_all_act_msg": false, "verif IED ": false," geo_enabled ": True," Class ": 1," Screen_name ":" Cz_\u5a77\u7ea6 "," Lang ":" Zh-cn "," Weih     AO ":" "," remark ":" "," Favourites_count ": 0," name ":" Cz_\u5a77\u7ea6 "," url ":" "," gender ":" F ",  "Created_at": "Thu 12:14:23 +0800", "Verified_type":-1, "following": false, "Pagefriends_count": 0, "Urank": 8}, "Geo": null, "Created_at": "Sun Dec 07:03:45 +0800", "Mlevel": 0, "Comments_co Unt ": 0}, {" Reposts_count ": 0," truncated ": false," text ":" #\u60c5\u4fc2\u963f\u54f2#\u4e70\u4e00\u5f20\u6f1 4\u5531\u4f1a\u95e8\u7968\uff0c\u5e26\u4e0a\u5c0f\u4f19\u4f34\uff0c\u7a7f\u7740\u4f1a\u670d\u62ff\u7740\u4f60\ U7684\u4e13\u8f91\u548c\u71c8\u724c\u5954\u8d74\uff0c\u4e0d\u7528\u79bb\u4f60\u5f88\u8fd1\u54ea\u6015\u5750\ U5728\u89d2\u843d\uff0c\u54ea\u6015\u4e0d\u80fd\u770b\u6e05\u53f0\u4e0a\u7684\u4f60\uff0c\u54ea\u6015\u4f60\u4e0d\u77e5\u9053\u4eba\u7fa4\u8fd8\u6709\u4e00\ U4e2a\u6211\u3002\u4f46\u53ea\u8981\u80fd\u542c\u89c1\u5e38\u5e38\u5728\u8033\u673a\u91cc\u51fa\u73b0\u7684\ U90a3\u4e2a\u58f0\u97f3\uff0c\u53ea\u8981\u80fd\u5750\u5728\u8fd9\u91cc\u4e00\u8d77\u4e3a\u4f60\u5450\u558a\ Uff0c\u53ea\u8981\u80fd\u548c\u4f60\u7ad9\u5728\u540c\u6837\u7684\u5929\u7a7a\u4e0b\u547c\u5438\u540c\u4e00\   U7247\u6c27\u6c14\uff0c\u8fd9\u6837\u5c31\u5df2\u7ecf\u5f88\u597d\u4e86 "," visible ": {" type ": 0," list_id ": 0 }, "in_reply_to_status_id": "", "Bmiddle_pic": "Http://ww4.sinaimg.cn/bmiddle/5d16267bjw1enp0tkhsxhj20c80klq39.jpg "," id ": 3792648903463367," thumbnail_pic ":" Http://ww4.sinaimg.cn/thumbnail/5d16267bjw1enp0tkhsxhj20c80klq39.jpg " , "Mid": "3792648903463367", "source": "<a href=\" http://app.weibo.com/t/feed/4p1gtp\ "rel=\" nofollow\ ">\u6735 \u552f\u5973\u6027\u624b\u673a</a> "," Attitudes_count ": 0," in_reply_to_sCreen_name ":", "Pic_urls": [{"Thumbnail_pic": "Http://ww4.sinaimg.cn/thumbnail/5d16267bjw1enp0tkhsxhj20c80kl Q39.jpg "}]," in_reply_to_user_id ":" "," darwin_tags ": []," favorited ": false," original_pic ":" http://w  W4.sinaimg.cn/large/5d16267bjw1enp0tkhsxhj20c80klq39.jpg "," Idstr ":" 3792648903463367 "," Source_type ": 1," User ": {"Bi_followers_count": 206, "Domain": "", "Avatar_large": "http://tp4.sinaimg.cn/1561732731/180/5709043270/0" , "Verified_source": "", "PType": 0, "Statuses_count": 57711, "allow_all_comment": false, "id": 156173     2731, "Verified_reason_url": "," City ":" 8 "," Province ":" Notoginseng "," Credit_score ":" Block_app ": 0,      "Follow_me": false, "Verified_reason": "", "Followers_count": 1487, "location": "\u5c71\u4e1c \u6d4e\u5b81", "Verified_trade": "", "Mbtype": 0, "Verified_source_url": "", "Profile_url": "u/1561732731", "Block_w Ord ": 0," AVATAR_HD ": "Http://ww3.sinaimg.cn/crop.100.0.318.318.1024/5d16267bjw8ellhct6ii9j20ef08ujs6.jpg", "star": 0, "description": "\u672c\u547d\u3001\u7537\u795e\uff1a\u5f35\u4fe1\u54f2/\u5973\u795e\uff1a\u6797\u4f9d\u6668/\u5076\u50cf:\ u74ca\u7464\u963f\u59e8/\u559c\u6b61\u7684\u9b54\u8853\u5e2b:\u5289\u8b19/\u559c\u6b61\u53f0\u7063\u5287 \U91D1 \u5eb8\u5287/"," Friends_count ": 951," Online_status ": 1," Mbrank ": 0," idstr ":" 1561732731 "," Profile     _image_url ":" http://tp4.sinaimg.cn/1561732731/50/5709043270/0 "," allow_all_act_msg ": false," verified ": false, "Geo_enabled": True, "Class": 1, "Screen_name": "\u88d9\u89d2\u98db\u63da0326\u4f9d\u5fc3\u54f2\u610f", "Lan G ":" Zh-tw "," Weihao ":" "," remark ":" "," Favourites_count ":", "" Name ":" \u88d9\u89d2\u98db\u63da0326\u4f  9d\u5fc3\u54f2\u610f "," url ":" Http://blog.sina.com.cn/qunjiao2quan "," Gender ":" F "," created_at ":" Sun Nov 01  17:38:36 +0800 "," Verified_type ": 220,   "Following": false, "Pagefriends_count": 0, "Urank": +, "geo": null, "Created_at": "Sun Dec 28 07:03 : +0800 "," Mlevel ": 0," Comments_count ": 0}]}


One of Sina Weibo data mining recipes: Login (API)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.