中山网站建设哪家便宜,个人网站可以备案,网站中转页怎么做,服务器建设网站以马蜂窝“普达措国家公园”为例#xff0c;其评论高达3000多条#xff0c;但这3000多条并非是完全向用户展示的#xff0c;向用户展示的只有5页#xff0c;数了一下每页15条评论#xff0c;也就是75条评论#xff0c;有点太少了吧#xff01; 因此想了个办法尽可能多爬…以马蜂窝“普达措国家公园”为例其评论高达3000多条但这3000多条并非是完全向用户展示的向用户展示的只有5页数了一下每页15条评论也就是75条评论有点太少了吧 因此想了个办法尽可能多爬取一些评论根据我对爬虫爬取数据法律法规的相关了解爬取看得到的数据是合法的而在评论最开始的这个地方有对评论的分类当然每个分类主题也是最多能看到5页内容但是肯定会比我们被动的只爬取5页多很多内容因此我们选择按主题分类去爬取评论。 点击上图中的全部右键检查或者按下F12去定位“全部” 把这个元素收起来就可以看到如下图这个li/li标签的列表里保存着分类名称、类型、id等如果比较多的话可以利用selenium的XPATH自动获取之后再套进代码里面由于我只演示一个例子并且分类标签也不多我就直接拿了这个列表放在代码里。 注意我们需要用到的是他的两个属性值
data-type、data-catagory
我存放的方式代码标注的分类id
data-typea [0,0,1,1,1,2,2,2,2,2,0]
data-catagoryb [0,2,13,12,11,134700810,173942219,112047583,112968615,143853527,1]
注意这个顺序a[i]与b[i]是按照图中框起来的li/li标签一一对应的顺序不能错。
点击Network按下CtrlR刷新一下 找到Name为poiCommentListApi?为首的如下图点击Headers红线画出来的内容是代码中comment_url代码标注①的地方根据你自己需要的进行替换。 下滑可以看到Request Headers中的‘Referer’和‘User-agent’两个参数根据你自己所需要的进行替换代码标注的②和③ 点击Payload如果是下面这种情况你就点击一下左边的分类标签任选一个在Name列表中一直往下滑找到Name为poiCommentListApi?为首的根据你的点击次数就会有多少个从后往前找看看规律 找到最后一个Name为poiCommentListApi?为首的点击Payload看一下这个params参数
所以对于同一个景点来说变化的参数有评论类别由type、catagory决定、页码取值范围1-5 分析完之后就可以写代码了
----美味的code
import re
import time
import requests
import pandas as pdcomment_url http://pagelet.mafengwo.cn/poi/pagelet/poiCommentListApi?
requests_headers {Referer: https://www.mafengwo.cn/poi/3110.html,User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36
}# Comment categories
a [0, 0, 1, 1, 1, 2, 2, 2, 2, 2, 0]
b [0, 2, 13, 12, 11, 134700810, 173942219, 112047583, 112968615, 143853527, 1]# Iterate through ten categories of comments
for i in range(11):# Get comments from five pages for each categoryfor num in range(1, 6):print(Fetching Page, num)requests_data {params: {poi_id:3110,type:%d,category:%d,page:%d,just_comment:1} % (a[i], b[i], num)}response requests.get(urlcomment_url, headersrequests_headers, paramsrequests_data)if 200 response.status_code:page response.content.decode(unicode-escape, ignore).encode(utf-8, ignore).decode(utf-8)page page.replace(\\/, /)date_pattern ra classbtn-comment _j_comment titleAdd CommentComment/a.*?\n.*?span classtime(.*?)/spandate_list re.compile(date_pattern).findall(page)star_pattern rspan classs-star s-star(\d)/spanstar_list re.compile(star_pattern).findall(page)comment_pattern rp classrev-txt([\s\S]*?)/pcomment_list re.compile(comment_pattern).findall(page)best_comment []for num in range(0, len(date_list)):date date_list[num]star star_list[num]comment comment_list[num]comment str(comment).replace(nbsp;, )comment comment.replace(br, )comment comment.replace(br /, )comment comment.replace(\n, )comment comment.replace(【, )comment comment.replace(】, )comment comment.replace(~, )comment comment.replace(*, )comment comment.replace(br /, )best_comment.append(comment)df pd.DataFrame({date: date_list, rating: star_list, comment: comment_list})df[comment] best_commentdf.to_csv(mafengwo.csv, modea, encodinggb18030, indexFalse, headerNone)print(Write successful)else:print(Fetch failed)既然都看到這裏了不如点个关注收藏再走咯