欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python爬取qq空間說(shuō)說(shuō)的實(shí)例代碼

 更新時(shí)間:2018年08月17日 10:31:50   作者:Zachary46  
這篇文章主要介紹了Python爬取qq空間說(shuō)說(shuō)的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧

具體代碼如下所示:

#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)') #這里填寫你的QQ號(hào)
  driver.find_element_by_id('p').clear()
  driver.find_element_by_id('p').send_keys('QQ密碼') #這里填寫你的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])
          #寫入本地文件
          #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)文章

  • 解決更改AUTH_USER_MODEL后出現(xiàn)的問(wèn)題

    解決更改AUTH_USER_MODEL后出現(xiàn)的問(wèn)題

    這篇文章主要介紹了解決更改AUTH_USER_MODEL后出現(xiàn)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-05-05
  • Python中常見(jiàn)的加密解密算法總結(jié)

    Python中常見(jiàn)的加密解密算法總結(jié)

    這篇文章主要為大家詳細(xì)介紹了Python中常見(jiàn)的一些加密解密算法的實(shí)現(xiàn),文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2023-03-03
  • python繪制風(fēng)場(chǎng)方向和大小quiver問(wèn)題

    python繪制風(fēng)場(chǎng)方向和大小quiver問(wèn)題

    這篇文章主要介紹了python繪制風(fēng)場(chǎng)方向和大小quiver問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • SVM算法的理解及其Python實(shí)現(xiàn)多分類和二分類問(wèn)題

    SVM算法的理解及其Python實(shí)現(xiàn)多分類和二分類問(wèn)題

    這篇文章主要介紹了SVM算法的理解及其Python實(shí)現(xiàn)多分類和二分類問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python中@property的理解和使用示例

    Python中@property的理解和使用示例

    這篇文章主要介紹了Python中@property的理解和使用,結(jié)合實(shí)例形式分析了Python中@property的功能、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-06-06
  • 學(xué)生如何注冊(cè)Pycharm專業(yè)版以及pycharm的安裝

    學(xué)生如何注冊(cè)Pycharm專業(yè)版以及pycharm的安裝

    這篇文章主要介紹了學(xué)生如何注冊(cè)Pycharm專業(yè)版以及pycharm的安裝,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 使用Python、TensorFlow和Keras來(lái)進(jìn)行垃圾分類的操作方法

    使用Python、TensorFlow和Keras來(lái)進(jìn)行垃圾分類的操作方法

    這篇文章主要介紹了如何使用Python、TensorFlow和Keras來(lái)進(jìn)行垃圾分類,這個(gè)模型在測(cè)試集上可以達(dá)到約80%的準(zhǔn)確率,可以作為一個(gè)基礎(chǔ)模型進(jìn)行后續(xù)的優(yōu)化,需要的朋友可以參考下
    2023-05-05
  • Python最大連續(xù)區(qū)間和動(dòng)態(tài)規(guī)劃

    Python最大連續(xù)區(qū)間和動(dòng)態(tài)規(guī)劃

    這篇文章主要介紹了Python最大連續(xù)區(qū)間和動(dòng)態(tài)規(guī)劃,文章圍繞Python最大連續(xù)區(qū)間和動(dòng)態(tài)規(guī)劃的相關(guān)資料展開(kāi)內(nèi)容,需要的小伙伴可以參考一下
    2022-01-01
  • Python操作Access數(shù)據(jù)庫(kù)基本步驟分析

    Python操作Access數(shù)據(jù)庫(kù)基本步驟分析

    這篇文章主要介紹了Python操作Access數(shù)據(jù)庫(kù)基本步驟,結(jié)合實(shí)例形式詳細(xì)分析了Python針對(duì)access操作的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-09-09
  • 基于Pyinstaller打包Python程序并壓縮文件大小

    基于Pyinstaller打包Python程序并壓縮文件大小

    這篇文章主要介紹了基于Pyinstaller打包Python程序并壓縮文件大小,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-05-05

最新評(píng)論