代码片段(1)
[代码] [Python]代码
01 | #coding:utf-8 |
02 | import requests |
03 | import base64 |
04 | import re |
05 | import urllib |
06 | import rsa |
07 | import json |
08 | import binascii |
09 | |
10 | username = 'xxxxxxx@qq.com' |
11 | password = 'xxxxxx' |
12 | |
13 | session = requests.Session() |
14 | |
15 | url_prelogin = 'http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.5)&_=1364875106625' |
16 | |
17 | url_login = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.5)' |
18 | #get servertime,nonce, pubkey,rsakv |
19 | |
20 | resp = session.get(url_prelogin) |
21 | json_data = re.search( '\((.*)\)' , resp.content).group( 1 ) |
22 | data = json.loads(json_data) |
23 | servertime = data[ 'servertime' ] |
24 | nonce = data[ 'nonce' ] |
25 | pubkey = data[ 'pubkey' ] |
26 | rsakv = data[ 'rsakv' ] |
27 | |
28 | # calc su |
29 | su = base64.b64encode(urllib.quote(username)) |
30 | #calc sp |
31 | |
32 | rsaPublickey = int (pubkey, 16 ) |
33 | key = rsa.PublicKey(rsaPublickey, 65537 ) |
34 | message = str (servertime) + '\t' + str (nonce) + '\n' + str (password) |
35 | sp = binascii.b2a_hex(rsa.encrypt(message,key)) |
36 |
37 | postdata = { |
38 | 'entry' : 'weibo' , |
39 | 'gateway' : '1' , |
40 | 'from' : '', |
41 | 'savestate' : '7' , |
42 | 'userticket' : '1' , |
43 | 'ssosimplelogin' : '1' , |
44 | 'vsnf' : '1' , |
45 | 'vsnval' : '', |
46 | 'su' : su, |
47 | 'service' : 'miniblog' , |
48 | 'servertime' : servertime, |
49 | 'nonce' : nonce, |
50 | 'pwencode' : 'rsa2' , |
51 | 'sp' : sp, |
52 | 'encoding' : 'UTF-8' , |
53 | 'url' : 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack' , |
54 | 'returntype' : 'META' , |
55 | 'rsakv' : rsakv, |
56 | } |
57 | |
58 | resp = session.post(url_login,data = postdata) |
59 | login_url = re.findall( 'replace\("(.*)"\)' ,resp.content) |
60 | #print login_url |
61 |
62 | resp = session.get(login_url[ 0 ]) |
63 | #print resp.content |
64 | |
65 | uid = re.findall( '"uniqueid":"(\d+)",' ,resp.content)[ 0 ] |
66 | url = "http://weibo.com/u/" + uid |
67 | resp = session.get(url) |
68 | #print resp.content |
69 |
70 | def decode_content(content): |
71 | result = re.findall( '<script>STK && STK.pageletM && STK.pageletM.view\((.*)\)<\/script>' ,content) |
72 | for i in result: |
73 | r = i.encode( "utf-8" ).decode( 'unicode_escape' ).encode( 'utf-8' ) |
74 | print r.replace( "\/" , "/" ) |
75 |
76 | url_search = "http://s.weibo.com/weibo/%s?topnav=1&wvr=5&b=1" % "刘德华" |
77 | resp = session.get(url_search) |
78 | print resp.content |