標籤:抓取 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開發爬蟲之動態網頁抓取篇:爬取部落格評論資料