python爬取馬蜂窩景點翻頁文字評論的實現(xiàn)-創(chuàng)新互聯(lián)

使用Chrome、python3.7、requests庫和VSCode進行爬取馬蜂窩黃鶴樓的文字評論(http://www.mafengwo.cn/poi/5426285.html)。

創(chuàng)新互聯(lián)自2013年起,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元松江做網(wǎng)站,已為上家服務(wù),為松江各地企業(yè)和個人服務(wù),聯(lián)系電話:13518219792

首先,我們復(fù)制一段評論,查看網(wǎng)頁源代碼,按Ctrl+F查找,發(fā)現(xiàn)沒有找到評論,說明評論內(nèi)容不在http://www.mafengwo.cn/poi/5426285.html頁面。

python 爬取馬蜂窩景點翻頁文字評論的實現(xiàn)


回到頁面,劃到評論列表,右鍵檢查,選擇Network,然后點擊后一頁翻頁,觀察Network里的變化,我們要爬的文件就在下面的某個文件里(主要找XHR和JS兩個模塊)。選擇Preview可以更好的讓我們尋找我們想要的文件,然后選擇Headers找到我們要爬的url。


經(jīng)過分析我們找到要爬取的url是http://pagelet.mafengwo.cn/poi/pagelet/poiCommentListApi?callback=jQuery18102698237405245767_1579401525334&params=%7B%22poi_id%22%3A%225426285%22%2C%22page%22%3A2%2C%22just_comment%22%3A1%7D&_ts=1579402072160&sn=20e98d65a0&=1579402072161
然而點進去是這樣的

python 爬取馬蜂窩景點翻頁文字評論的實現(xiàn)


這個時候?qū)Ρ纫幌逻@兩個頁面的Request Headers,發(fā)現(xiàn)原頁面多了個Refer參數(shù)


原頁面



然后看一下請求get請求需要的參數(shù)Query String Parameters,其中poi_id是景點id,page是評論頁面(翻頁只用改變page的值就行)。

python 爬取馬蜂窩景點翻頁文字評論的實現(xiàn)

import re
import time
import requests
#評論內(nèi)容所在的url,?后面是get請求需要的參數(shù)內(nèi)容
comment_url='http://pagelet.mafengwo.cn/poi/pagelet/poiCommentListApi?'

requests_headers={
  'Referer': 'http://www.mafengwo.cn/poi/5426285.html',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
}#請求頭

for num in range(1,6):
  requests_data={
    'params': '{"poi_id":"5426285","page":"%d","just_comment":1}' % (num)  #經(jīng)過測試只需要用params參數(shù)就能爬取內(nèi)容
    }
  response =requests.get(url=comment_url,headers=requests_headers,params=requests_data)
  if 200==response.status_code:
    page = response.content.decode('unicode-escape', 'ignore').encode('utf-8', 'ignore').decode('utf-8')#爬取頁面并且解碼
    page = page.replace('\\/', '/')#將\/轉(zhuǎn)換成/
    #日期列表
    date_pattern = r'<a class="btn-comment _j_comment" title="添加評論">評論</a>.*?\n.*?<span class="time">(.*?)</span>'
    date_list = re.compile(date_pattern).findall(page)
    #星級列表
    star_pattern = r'<span class="s-star s-star(\d)"></span>'
    star_list = re.compile(star_pattern).findall(page)
    #評論列表
    comment_pattern = r'<p class="rev-txt">([\s\S]*?)</p>'
    comment_list = re.compile(comment_pattern).findall(page)
    for num in range(0, len(date_list)):
      #日期
      date = date_list[num]
      #星級評分
      star = star_list[num]
      #評論內(nèi)容,處理一些標簽和符號
      comment = comment_list[num]
      comment = str(comment).replace('&nbsp;', '')
      comment = comment.replace('<br>', '')
      comment = comment.replace('<br />', '')
      print(date+"\t"+star+"\t"+comment)
  else:
    print("爬取失敗")

新聞名稱:python爬取馬蜂窩景點翻頁文字評論的實現(xiàn)-創(chuàng)新互聯(lián)
URL鏈接:http://muchs.cn/article26/hegjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、自適應(yīng)網(wǎng)站、Google、標簽優(yōu)化用戶體驗、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司