Python開發爬蟲之動態網頁抓取篇:爬取部落格評論資料

來源:互聯網
上載者:User

標籤:抓取   san   window   ati   檢查   amp   網頁抓取   實踐   string   

以爬取《Python 網路爬蟲:從入門到實踐》一書作者的個人部落格評論為例。網址:http://www.santostang.com/2017/03/02/hello-world/

1)“抓包”:找到真實的資料地址

 

右鍵點擊“檢查”,點擊“network”,選擇“js”。重新整理一下頁面,選中頁面重新整理時返回的資料list?callback....這個js檔案。右邊再選中Header。

其中,Request URL即是真實的資料地址。

在此狀態下滾動滑鼠滾輪可發現User-Agent。

2)相關代碼:

import requestsimport jsonheaders={‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘}link="https://api-zero.livere.com/v1/comments/list?callback=jQuery112405600294326674093_1523687034324&limit=10&offset=2&repSeq=3871836&requestPath=%2Fv1%2Fcomments%2Flist&consumerSeq=1020&livereSeq=28583&smartloginSeq=5154&_=1523687034329"r=requests.get(link,headers=headers)# 擷取 json 的 stringjson_string = r.textjson_string = json_string[json_string.find(‘{‘):-2]json_data=json.loads(json_string)comment_list=json_data[‘results‘][‘parents‘]for eachone in comment_list:    message=eachone[‘content‘]    print(message)

據觀察,在真實的資料地址中的offset是頁數。

爬取所有頁面的評論:

import requestsimport jsondef single_page_comment(link):    headers={‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36‘}       r=requests.get(link,headers=headers)    # 擷取 json 的 string    json_string = r.text    json_string = json_string[json_string.find(‘{‘):-2]    json_data=json.loads(json_string)    comment_list=json_data[‘results‘][‘parents‘]    for eachone in comment_list:        message=eachone[‘content‘]        print(message)        for page in range(1,4):    link1="https://api-zero.livere.com/v1/comments/list?callback=jQuery112405600294326674093_1523687034324&limit=10&offset="    link2="&repSeq=3871836&requestPath=%2Fv1%2Fcomments%2Flist&consumerSeq=1020&livereSeq=28583&smartloginSeq=5154&_=1523687034329"    page_str=str(page)    link=link1+page_str+link2    print(link)    single_page_comment(link)

 

 參考書目:唐松,來自《Python 網路爬蟲:從入門到實踐》

 

Python開發爬蟲之動態網頁抓取篇:爬取部落格評論資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.