Python爬取qq空間說(shuō)說(shuō)的實(shí)例代碼
具體代碼如下所示:
#coding:utf-8
#!/usr/bin/python3
from selenium import webdriver
import time
import re
import importlib2
import sys
importlib2.reload(sys)
def startSpider():
driver = webdriver.Chrome('/Users/zachary/zachary/chromedriver.exe') #這個(gè)是chormedriver的地址
driver.get('https://qzone.qq.com/')
driver.switch_to.frame('login_frame')
driver.find_element_by_id('switcher_plogin').click()
driver.find_element_by_id('u').clear()
driver.find_element_by_id('u').send_keys('QQ號(hào)') #這里填寫(xiě)你的QQ號(hào)
driver.find_element_by_id('p').clear()
driver.find_element_by_id('p').send_keys('QQ密碼') #這里填寫(xiě)你的QQ密碼
driver.find_element_by_id('login_button').click()
time.sleep(2)
#設(shè)置爬取內(nèi)容保存路徑
f = open('/Users/zachary/Documents/shuoshuo.txt','w')
#---------------獲得g_qzonetoken 和 gtk
html = driver.page_source
'''g_qzonetoken=re.search('window\.g_qzonetoken = \(function\(\)\{ try\{return (.*?);\} catch\(e\)',html)#從網(wǎng)頁(yè)源碼中提取g_qzonetoken'''
g_qzonetoken = "e794139a284d6ea9e0b26826e541b55df37d0667a3544f534de25aebdb64628d3ab75e1d7104bbb22a"
cookie = {}#初始化cookie字典
for elem in driver.get_cookies():#取cookies
cookie[elem['name']] = elem['value']
gtk=getGTK(cookie)#通過(guò)getGTK函數(shù)計(jì)算gtk
#print(g_qzonetoken)
#print(gtk)
#--------------獲得好友列表 注意下面的鏈接
driver.get('https://user.qzone.qq.com/proxy/domain/r.qzone.qq.com/cgi-bin/tfriend/friend_hat_get.cgi?hat_seed=1&uin=你的QQ號(hào)fupdate=1&g_tk='+str(gtk)+'&qzonetoken='+str(g_qzonetoken)+'&g_tk='+str(gtk))
friend_list = driver.page_source
friend_list = str( friend_list )
abtract_pattern = re.compile('\"(.\d*)\":\{\\n"realname":"(.*?)"}',re.S)
QQ_name_list = re.findall(abtract_pattern,str(friend_list)) #數(shù)組
print(QQ_name_list)
numList=dict()# numList => (QQnum:QQname) #列表
for i in QQ_name_list:
numList[str(i[0])]=str(i[1])
begin = 0
last_source = ""
tag = 1
first = 0
firstTime=""
#如果要爬取自己的說(shuō)說(shuō),手動(dòng)添加自己的qq號(hào)
#numList['你的qq號(hào)']='你的名字'
#print(numList)
for key in numList.keys():
QQnum = key
QQname = numList[QQnum]
if QQnum == "好友qq號(hào)": #根據(jù)qq號(hào)查找指定好友說(shuō)說(shuō)
count = 1
begin = 0
while tag==1 :
#-------------進(jìn)入好友說(shuō)說(shuō)頁(yè)面 #'+QQnum+' '+str(begin)+'
#print("Begin:"+str(begin))
driver.get('https://user.qzone.qq.com/proxy/domain/taotao.qq.com/cgi-bin/emotion_cgi_msglist_v6?uin='+QQnum+'&ftype=0&sort=0&pos='+str(begin)+'&num=40&replynum=200&g_tk='+str(gtk)+'&callback=_preloadCallback&code_version=1&format=jsonp&need_private_comment=1&qzonetoken='+str(g_qzonetoken)+'&g_tk='+str(gtk))
try:
msg_list_json = driver.page_source
except:
begin = begin + 40
continue
msg_list_json = str(msg_list_json)
if last_source==msg_list_json :
break
else:
last_source=msg_list_json
#檢測(cè)是否沒(méi)有權(quán)限訪問(wèn)
abtract_pattern = re.compile(',"message":"(.*?)","name":',re.S)
message = re.findall(abtract_pattern,str(msg_list_json))
if message!=[]:
if str(message[0])=='對(duì)不起,主人設(shè)置了保密,您沒(méi)有權(quán)限查看':#對(duì)不起,主人設(shè)置了保密,您沒(méi)有權(quán)限查看
break
#print(msg_list_json)
#解析JSON
#webDriver沒(méi)有現(xiàn)成的JSON解析器,所以采用獲取源碼的方式,然后使用正則表達(dá)式獲取具體細(xì)節(jié)
msg_list_json = msg_list_json.split("msglist")[1]#拆分json,縮小范圍,也能加快解析速度
msg_list_json = msg_list_json.split("smoothpolicy")[0]
msg_list_json = msg_list_json.split("commentlist")[1:]
#說(shuō)說(shuō)動(dòng)態(tài)分4種:1、文字說(shuō)說(shuō)(或帶有配圖的文字說(shuō)說(shuō))
# 2、只有圖片的說(shuō)說(shuō)
# 3、轉(zhuǎn)發(fā),并配有文字
# 4、轉(zhuǎn)發(fā),不配文字
for text in msg_list_json:
# 1、先檢查說(shuō)說(shuō),用戶是否發(fā)送了文字,如果沒(méi)有文字,正則表達(dá)式匹配無(wú)效
abtract_pattern = re.compile('\}\],"content":"(.*?)","createTime":"(.*?)","created_time":(.*?),"',re.S)
msg_time = re.findall(abtract_pattern,str(text))
if msg_time!=[]:
# 2、如果作者說(shuō)說(shuō)有文字,那么檢查是否有轉(zhuǎn)發(fā)內(nèi)容
msg = str(msg_time[0][0])
sendTime = str(msg_time[0][1])
abtract_pattern = re.compile('\}\],"content":"(.*?)"},"rt_createTime":"(.*?)","',re.S)
text = text.split("created_time")[1]
msg_time2 = re.findall(abtract_pattern,str(text))
#合并發(fā)送內(nèi)容 格式:評(píng)論+轉(zhuǎn)發(fā)內(nèi)容
if msg_time2!=[]:
msg = msg +" 轉(zhuǎn)發(fā)內(nèi)容:"+str(msg_time2[0][0])
else:
# 3、說(shuō)說(shuō)內(nèi)容為空,檢查是否為 =>只有圖片的說(shuō)說(shuō) or 轉(zhuǎn)發(fā),不配文字
#獲取正文發(fā)送時(shí)間 (發(fā)送時(shí)間分為:正文發(fā)送時(shí)間 or 轉(zhuǎn)發(fā)時(shí)間)
abtract_pattern = re.compile('"conlist":null,"content":"","createTime":"(.*?)",',re.S)
msgNull_time = re.findall(abtract_pattern,str(text))
if msgNull_time!=[]:
#如果有正文發(fā)送時(shí)間,那么就是這條說(shuō)說(shuō)僅含有圖片 =>只有圖片的說(shuō)說(shuō)
msg = "圖片"
sendTime = str(msgNull_time[0])
else:
#如果沒(méi)有正文發(fā)送時(shí)間,那么就是說(shuō)這條說(shuō)為 =>轉(zhuǎn)發(fā),不配文字
abtract_pattern = re.compile('\}\],"content":"(.*?)"},"rt_createTime":"(.*?)","',re.S)
msg_time = re.findall(abtract_pattern,str(text))
msg =" 轉(zhuǎn)發(fā)內(nèi)容:"+str(msg_time[0][0])
sendTime = str(msg_time[0][1])
#寫(xiě)入本地文件
#f.write('{},{},{},{}\n'.format(str(QQname),str(QQnum),sendTime,msg))
print(str(count)+" : "+str(QQname)+" : "+str(QQnum)+" : "+sendTime+" : "+msg)
count = count + 1
begin = begin + 40
def getGTK(cookie):
hashes = 5381
for letter in cookie['p_skey']:
hashes += (hashes << 5) + ord(letter)
return hashes & 0x7fffffff
startSpider()
print("爬取結(jié)束")
總結(jié)
以上所述是小編給大家介紹的Python爬取qq空間說(shuō)說(shuō)的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Pycharm中python調(diào)用另一個(gè)文件類(lèi)或者函數(shù)
本文主要介紹了Pycharm中python調(diào)用另一個(gè)文件類(lèi)或者函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
python實(shí)現(xiàn)可以斷點(diǎn)續(xù)傳和并發(fā)的ftp程序
斷點(diǎn)續(xù)傳和并發(fā)是現(xiàn)在很多ftp程序都支持的功能,如果我們用python如何來(lái)做斷點(diǎn)續(xù)傳和并發(fā)了,今天來(lái)看一篇python實(shí)現(xiàn)斷點(diǎn)續(xù)傳和并發(fā)的ftp程序例子吧,具體如下。2016-09-09
利用Python過(guò)濾相似文本的簡(jiǎn)單方法示例
這篇文章主要給大家介紹了關(guān)于利用Python過(guò)濾相似文本的簡(jiǎn)單方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
python實(shí)現(xiàn)簡(jiǎn)易名片管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)簡(jiǎn)易名片管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
OpenCV-Python實(shí)現(xiàn)多模板匹配
模板匹配就是在一幅圖像中尋找另一幅模板圖像最匹配,本文主要實(shí)現(xiàn)了多模板匹配,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Python函數(shù)默認(rèn)參數(shù)常見(jiàn)問(wèn)題及解決方案
這篇文章主要介紹了Python函數(shù)默認(rèn)參數(shù)常見(jiàn)問(wèn)題及解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
python編程使用PyQt制作預(yù)覽窗口游戲中的小地圖
這篇文章主要為大家介紹了python使用PyQt制作預(yù)覽窗口游戲中的小地圖實(shí)現(xiàn)示例過(guò)程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-10-10

