主文件以下:
#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 = "https://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su=&rsakt=mod&client=ssologin.js(v1.4.11)&_=1379834957683"
self.loginUrl = "https://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="https://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="https://s.weibo.com/user/&auth=ord&age=22y&gender=women®ion=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是https://s.weibo.com/weibo/s 那末 这句话的的后果就是 https://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("[email protected]", "XXXXXXXX")#邮箱(账号)、密码
if weiboLogin.Login() == True:
print "登陆胜利!"
myurl="https://api.weibo.com/2/statuses/timeline_batch.json?source=1675437817&uids=5029941840"
htmlContent = urllib2.urlopen(myurl).read()
print htmlContent#coding=utf-8
import requests
import urllib