使用Chrome、python3.7、requests庫(kù)和VSCode進(jìn)行爬取馬蜂窩黃鶴樓的文字評(píng)論(http://www.mafengwo.cn/poi/5426285.html)。
首先,我們復(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è)面。
回到頁(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¶ms=%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)去是這樣的
這個(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的值就行)。
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(' ', '') 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)
猜你還喜歡下面的內(nèi)容