设为首页 收藏本站
查看: 979|回复: 0

[经验分享] 基于python的新浪微博模拟登陆

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-2-9 09:43:28 | 显示全部楼层 |阅读模式
                      主文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#coding=utf-8
import requests
import urllib
import urllib2
import cookielib  
import WeiboEncode
import WeiboSearch
import time
import re
import random
import httplib
class WeiboLogin:
def __init__(self, user, pwd, enableProxy = False):#构造方法,参数依次是自身、用户、密码、是否使用代理服务器
"初始化WeiboLogin,enableProxy表示是否使用代理服务器,默认关闭"  
print "Initializing WeiboLogin..."
self.userName = user
self.passWord = pwd
self.enableProxy = enableProxy
self.serverUrl = "http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.11)&_=1379834957683"
self.loginUrl = "http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.11)"
self.postHeader = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0'}
#用户代理 User Agent,是指浏览器,它的信息包括硬件平台、系统软件、应用软件和用户个人偏好。
def Login(self):#登陆程序
"登陆程序"  
self.EnableCookie(self.enableProxy)#cookie或代理服务器配置
serverTime, nonce, pubkey, rsakv = self.GetServerTime()#登陆的第一步
postData = WeiboEncode.PostEncode(self.userName, self.passWord, serverTime, nonce, pubkey, rsakv)#加密用户和密码
print "Post data length:\n", len(postData)  
req = urllib2.Request(self.loginUrl, postData, self.postHeader)#构造网络请求
print "Posting request..."
result = urllib2.urlopen(req)#发出网络请求
text = result.read()
try:
loginUrl = WeiboSearch.sRedirectData(text)#解析重定位结果(登陆后自动跳转到的页面)
urllib2.urlopen(loginUrl)
except:
print 'Login error!'
return False
print 'Login sucess!'
return True
def EnableCookie(self, enableProxy):#"Enable cookie & proxy (if needed)."
cookiejar = cookielib.LWPCookieJar()#建立cookie
cookie_support = urllib2.HTTPCookieProcessor(cookiejar)  
#HTTPCookieProcessor instances have one attribute:
#HTTPCookieProcessor.cookiejar (The cookielib.CookieJar in which cookies are stored.)
if enableProxy:
proxy_support = urllib2.ProxyHandler({'http':'59.59.100.123:8118'})#使用代理
opener = urllib2.build_opener(proxy_support, cookie_support, urllib2.HTTPHandler)#Return an OpenerDirector instance
#The OpenerDirector class opens URLs via BaseHandlers chained together.
print "Proxy enabled"
else:
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)#构建cookie对应的opener
def GetServerTime(self):#"Get server time and nonce, which are used to encode the password"
#在摘要认证中服务器让客户选一个随机数(称作”nonce“),然后浏览器使用一个单向的加密函数生成一个消息摘要(message #digest),该摘要是关于用户名、密码、给定的nonce值、HTTP方法,以及所请求的URL。
print "Getting server time and nonce..."
serverData = urllib2.urlopen(self.serverUrl).read()#得到网页内容
print serverData
try:
serverTime, nonce, pubkey, rsakv = WeiboSearch.sServerData(serverData)#解析得到serverTime,nonce等
return serverTime, nonce, pubkey, rsakv
except:
print 'Get server time & nonce error!'
return None
def fetch_weibo(id, filename):#不借助API取回微博列表,但只有前几条,参数分别为用户ID、文件名
target = open(filename, 'a')
myurl='http://weibo.com/u/'+id
line = urllib2.urlopen(myurl).read()
target.write(line)
if re.search(r'\"WB_detail', line):
print "success"
p = re.compile(r'\"WB_detail\"')
linelist = p.split(line)
for fraction in linelist:
matchObj = re.search(r'nick-name=\".+?\">\\n +(.+?)<', fraction)
if matchObj:
target.write(matchObj.group(1))
target.write("\n")
def fetchqueryresult():#本方法可取回微博找人的查询结果
myurl="http://s.weibo.com/user/&auth=ord&age=22y&gender=women&region=custom:33:1&page="#找人页面的url
target = open("filename", 'a')#输出文件名称
for i in range(37,51):#起止页码
line = urllib2.urlopen(myurl).read()
while re.search(r'ids\=(\d+?)\\', line):
matchObj = re.search(r'ids\=(\d+?)\\', line)
print matchObj.group(1)
target.write(matchObj.group(1))
target.write("\n")
p = re.compile(r''+matchObj.group(1))
linelist = p.split(line)
line = linelist[len(linelist)-1]
print i
time.sleep(2+random.random());
def getjson():#本方法可调用微博API,取回已登录用户的微博列表
      
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Firefox/24.0'}#定义一些文件头
url = "https://api.weibo.com/2/statuses/user_timeline.json"  # 这里是url
your_param = {'source': '1675437817'}  # 这里是请求参数!
result = requests.get(url, params=your_param)  # 发送请求,如果url是http://s.weibo.com/weibo/s 那么 这句话的的效果就是 http://s.weibo.com/weibo/s?Refer=sina_index
result_final = result.text  #这样就获取到了你发送的这个URL + 参数 之后的结果
print result.text
if __name__ == '__main__':
#if the python interpreter is running that module (the source file) as the main program,
#it sets the special __name__ variable to have a value #"__main__".
#If this file is being imported from another module,
#__name__ will be set to the module's name.
weiboLogin = WeiboLogin('tanglie23@163.com', 'XXXXXXXX')#邮箱(账号)、密码
if weiboLogin.Login() == True:
print "登陆成功!"
myurl="http://api.weibo.com/2/statuses/timeline_batch.json?source=1675437817&uids=5029941840"
htmlContent = urllib2.urlopen(myurl).read()
print htmlContent



另外要用到的两个类是WeiboSearch.py和WeiboEncode.py。
WeiboEncode.py代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#coding=utf-8
import urllib
import base64
import rsa
import binascii  
def PostEncode(userName, passWord, serverTime, nonce, pubkey, rsakv):
    "Used to generate POST data"
    encodedUserName = GetUserName(userName)#用户名使用base64加密
    encodedPassWord = get_pwd(passWord, serverTime, nonce, pubkey)#目前密码采用rsa加密
    postPara = {
        'entry': 'weibo',
        'gateway': '1',
        'from': '',
        'savestate': '7',
        'userticket': '1',
        'ssosimplelogin': '1',
        'vsnf': '1',
        'vsnval': '',
        'su': encodedUserName,
        'service': 'miniblog',
        'servertime': serverTime,
        'nonce': nonce,
        'pwencode': 'rsa2',
        'sp': encodedPassWord,
        'encoding': 'UTF-8',
        'prelt': '115',
        'rsakv': rsakv,     
        'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
        'returntype': 'META'
    }
    postData = urllib.urlencode(postPara)#网络编码
    return postData
  
def GetUserName(userName):
    "Used to encode user name"
    userNameTemp = urllib.quote(userName)
    userNameEncoded = base64.encodestring(userNameTemp)[:-1]
    return userNameEncoded  
  
def get_pwd(password, servertime, nonce, pubkey):
    rsaPublickey = int(pubkey, 16)
    key = rsa.PublicKey(rsaPublickey, 65537) #创建公钥
    message = str(servertime) + '\t' + str(nonce) + '\n' + str(password) #拼接明文js加密文件中得到
    passwd = rsa.encrypt(message, key) #加密
    passwd = binascii.b2a_hex(passwd) #将加密信息转换为16进制。
    return passwd




WeiboSearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#coding=utf-8
import re
import json  
def sServerData(serverData):#解析得到serverTime,nonce等
    "Search the server time & nonce from server data"
    p = re.compile('\((.*)\)') #re.compile 可以把正则表达式编译成一个正则表达式对象
    jsonData = p.search(serverData).group(1) #查找
    data = json.loads(jsonData) #对encodedjson进行decode,得到原始数据,需要使用json.loads()函数
    serverTime = str(data['servertime'])
    nonce = data['nonce']
    pubkey = data['pubkey']#
    rsakv = data['rsakv']#
    print "Server time is:", serverTime
    print "Nonce is:", nonce
    return serverTime, nonce, pubkey, rsakv
  
def sRedirectData(text):
    p = re.compile('location\.replace\([\'"](.*?)[\'"]\)')
    loginUrl = p.search(text).group(1)
    print 'loginUrl:',loginUrl
    return loginUrl



目前该爬虫可以自动登录,以及调用新浪微博的普通API。但是批量取回他人的微博需要高级授权,目前正在申请。
                   


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-42231-1-1.html 上篇帖子: Python 操作LDAP实现用户统一认证密码修改功能 下篇帖子: python simplejson模块浅谈 新浪微博 python
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表