成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

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

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

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

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

python 爬取馬蜂窩景點(diǎn)翻頁(yè)文字評(píng)論的實(shí)現(xiàn)


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


經(jīng)過(guò)分析我們找到要爬取的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
然而點(diǎn)進(jìn)去是這樣的

python 爬取馬蜂窩景點(diǎn)翻頁(yè)文字評(píng)論的實(shí)現(xiàn)


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


原頁(yè)面



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

python 爬取馬蜂窩景點(diǎn)翻頁(yè)文字評(píng)論的實(shí)現(xiàn)

import re
import time
import requests
#評(píng)論內(nèi)容所在的url,?后面是get請(qǐng)求需要的參數(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'
}#請(qǐng)求頭

for num in range(1,6):
  requests_data={
    'params': '{"poi_id":"5426285","page":"%d","just_comment":1}' % (num)  #經(jīng)過(guò)測(cè)試只需要用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')#爬取頁(yè)面并且解碼
    page = page.replace('\\/', '/')#將\/轉(zhuǎn)換成/
    #日期列表
    date_pattern = r'<a class="btn-comment _j_comment" title="添加評(píng)論">評(píng)論</a>.*?\n.*?<span class="time">(.*?)</span>'
    date_list = re.compile(date_pattern).findall(page)
    #星級(jí)列表
    star_pattern = r'<span class="s-star s-star(\d)"></span>'
    star_list = re.compile(star_pattern).findall(page)
    #評(píng)論列表
    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]
      #星級(jí)評(píng)分
      star = star_list[num]
      #評(píng)論內(nèi)容,處理一些標(biāo)簽和符號(hào)
      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爬取馬蜂窩景點(diǎn)翻頁(yè)文字評(píng)論的實(shí)現(xiàn)-創(chuàng)新互聯(lián)
URL鏈接:http://jinyejixie.com/article26/hegjg.html

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

廣告

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

成都app開發(fā)公司
浪卡子县| 宜州市| 建德市| 木里| 明溪县| 江永县| 全州县| 昌邑市| 抚顺县| 昌宁县| 茌平县| 阿勒泰市| 舒城县| 沂水县| 伊通| 卫辉市| 安宁市| 穆棱市| 仁化县| 庄浪县| 泗洪县| 固始县| 剑川县| 都匀市| 锡林郭勒盟| 神池县| 吉安市| 福清市| 宿松县| 宁安市| 和顺县| 柯坪县| 息烽县| 吴川市| 长丰县| 平定县| 论坛| 梁河县| 崇明县| 奎屯市| 沁阳市|