python 爬取马蜂窝景点翻页文字评论的实现

yipeiwu_com6年前Python爬虫

使用Chrome、python3.7、requests库和VSCode进行爬取马蜂窝黄鹤楼的文字评论(http://www.mafengwo.cn/poi/5426285.html)。

首先,我们复制一段评论,查看网页源代码,按Ctrl+F查找,发现没有找到评论,说明评论内容不在http://www.mafengwo.cn/poi/5426285.html页面。

在这里插入图片描述

回到页面,划到评论列表,右键检查,选择Network,然后点击后一页翻页,观察Network里的变化,我们要爬的文件就在下面的某个文件里(主要找XHR和JS两个模块)。选择Preview可以更好的让我们寻找我们想要的文件,然后选择Headers找到我们要爬的url。

在这里插入图片描述

在这里插入图片描述

经过分析我们找到要爬取的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
然而点进去是这样的

在这里插入图片描述

这个时候对比一下这两个页面的Request Headers,发现原页面多了个Refer参数

原页面

在这里插入图片描述

在这里插入图片描述

然后看一下请求get请求需要的参数Query String Parameters,其中poi_id是景点id,page是评论页面(翻页只用改变page的值就行)。

在这里插入图片描述

import re
import time
import requests
#评论内容所在的url,?后面是get请求需要的参数内容
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)  #经过测试只需要用params参数就能爬取内容
    }
  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('\\/', '/')#将\/转换成/
    #日期列表
    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]
      #评论内容,处理一些标签和符号
      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设计】。

相关文章

python协程gevent案例 爬取斗鱼图片过程解析

python协程gevent案例 爬取斗鱼图片过程解析

分析 分析网站寻找需要的网址 用谷歌浏览器摁F12打开开发者工具,然后打开斗鱼颜值分类的页面,如图: 在里面的请求中,最后发现它是以ajax加载的数据,数据格式为json,如图: 圈...

Python使用requests及BeautifulSoup构建爬虫实例代码

Python使用requests及BeautifulSoup构建爬虫实例代码

本文研究的主要是Python使用requests及BeautifulSoup构建一个网络爬虫,具体步骤如下。 功能说明 在Python下面可使用requests模块请求某个url获取响应...

深入理解Python爬虫代理池服务

深入理解Python爬虫代理池服务

在公司做分布式深网爬虫,搭建了一套稳定的代理池服务,为上千个爬虫提供有效的代理,保证各个爬虫拿到的都是对应网站有效的代理IP,从而保证爬虫快速稳定的运行,当然在公司做的东西不能开源出来。...

python爬取网站数据保存使用的方法

编码问题因为涉及到中文,所以必然地涉及到了编码的问题,这一次借这个机会算是彻底搞清楚了。问题要从文字的编码讲起。原本的英文编码只有0~255,刚好是8位1个字节。为了表示各种不同的语言,自...

简单的Python抓taobao图片爬虫

写了一个抓taobao图片的爬虫,全是用if,for,while写的,比较简陋,入门作品。从网页http://mm.taobao.com/json/request_top_list.htm...