Python爬虫系列 - 初探:爬取旅游评论
Python爬虫目前是基于requests包,下面是该包的文档,查一些资料还是比较方便。
http://docs.python-requests.org/en/master/
POST发送内容格式
爬取某旅游网站的产品评论,通过分析,获取json文件需要POST指令。简单来说:
- GET是将需要发送的信息直接添加在网址后面发送
- POST方式是发送一个另外的内容到服务器
那么通过POST发送的内容可以大概有三种,即form、json和multipart,目前先介绍前两种
1.content in form
Content-Type: application/x-www-form-urlencoded
将内容放入dict,然后传递给参数data即可。
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.post(url, data=payload)
2. content in json
Content-Type: application/json
将dict转换为json,传递给data参数。
payload = {'some': 'data'}
r = requests.post(url, data=json.dumps(payload))
或者将dict传递给json参数。
payload = {'some': 'data'}
r = requests.post(url, json=payload
HTTP Hearder概述
A new request may need type(eg: POST), URL, request Headers and request Body. Now let's talk about the request body of a new POST request.
Reference: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Accept
It can be used to specify certain media types which are acceptable for the response.
The asterisk "*" character means all types. For example, "*/*" indicating all media types and "type/*" indicating all subtypes of that type.
";" "q" "=" qvalue is a relative degree. The default "q" is 1.
Accept: audio/*; q=0.2, audio/basic
If more than one media range applies to a given type, the most specific reference has precedence.
Accept: text/*, text/html, text/html;level=1, */*
In this example, "text/html;level=1" has the highest precedence.
Content-Length
the size of the entity-body that would have been sent had the request been a GET.
For example, The form data is like this:
type: all
currentPage: 3
productId:
And the Request Body you send is like this:
type=all¤tPage=3&productId=
So the Content-Length is 33.
User-Agent
Search the Internet for different User-Agents.
然后贴一下简单的代码供参考。
import requests
import json def getCommentStr():
url = r"https://package.com/user/comment/product/queryComments.json" header = {
'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0',
'Accept': r'application/json, text/javascript, */*; q=0.01',
'Accept-Language': r'en-US,en;q=0.5',
'Accept-Encoding': r'gzip, deflate, br',
'Content-Type': r'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': r'XMLHttpRequest',
'Content-Length': '',
'DNT': '',
'Connection': r'keep-alive',
'TE': r'Trailers'
} params = {
'pageNo': '',
'pageSize': '',
'productId': '',
'rateStatus': 'ALL',
'type': 'all'
} r = requests.post(url, headers = header, data = params)
print(r.text) getCommentStr()
小技巧
- 对于cookies,感觉可以用浏览器的编辑功能,逐步删除每次发送的cookies信息,判断哪些是没有用的?
- 对于测试代码阶段,我还是比较习惯于将爬取的数据存为str,也算是为了服务器减负吧。
爬取信息处理
爬取信息处理主要讲Beautifulsoup库和正则表达式(Regular Expression)
1. BeautifulSoup
bs4的官方文档
https://www.crummy.com/software/BeautifulSoup/bs4/doc/index.zh.html
首先在Ternimal安装 pip install bs4 ,同时也可以安装lxml解析器 pip install lxml ,或者html5lib解析器。
soup = bs4.BeautifulSoup(t,'lxml')
tagList = soup.find_all('div', attrs={'class': 'content'})
tagList = soup.find_all('div', attrs={'class': re.compile("(content)|()")})
其中t是需要解析的文本,lxml是解析器。
tagList接收的是div标签下class="content"的标签内容,其中可以运用正则表达式对象。
2. 正则表达式
正则表达式使用前先 import re ,基本语法见笔记。
提取匹配信息
对目标文本t匹配
useful = re.findall(r'有用<em>\d+</em>',t)
构造正则表达式对象,并进行使用
usefulRE = re.compile('有用<em>\d+</em>')
useful = usefulRE.findall(t)
替换匹配信息
replace()函数替换文本
newUseful.append(useful[i].replace('有用<em>','').replace('</em>',''))
正则表达式替换文本
newScoreA.append(re.sub(r'[^\d+]','',scoreA[i]))
Python爬虫系列 - 初探:爬取旅游评论的更多相关文章
- Python爬虫系列之爬取美团美食板块商家数据(二)
今天为大家重写一个美团美食板块小爬虫,说不定哪天做旅游攻略的时候也可以用下呢.废话不多说,让我们愉快地开始吧~ 开发工具 Python版本:3.6.4 相关模块: requests模块: argpar ...
- Python爬虫系列之爬取美团美食板块商家数据(一)
主要思路 目的: 根据输入的城市名,爬取该城市美团美食板块所有商家的数据.数据包括: 店名.评分.评论数量.均价.地址, 并将这些数据存入Excel中. 最后尝试对爬取到的数据做一个简单的分析. 克服 ...
- python爬虫系列之爬取多页gif图像
python爬取多页gif图像 作者:vpoet mail:vpoet_sir@163.com #coding:utf-8 import urllib import ur ...
- 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神
原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...
- Python爬虫实例:爬取B站《工作细胞》短评——异步加载信息的爬取
很多网页的信息都是通过异步加载的,本文就举例讨论下此类网页的抓取. <工作细胞>最近比较火,bilibili 上目前的短评已经有17000多条. 先看分析下页面 右边 li 标签中的就是短 ...
- Python爬虫实例:爬取猫眼电影——破解字体反爬
字体反爬 字体反爬也就是自定义字体反爬,通过调用自定义的字体文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容的. 现在貌似不少网 ...
- Python爬虫实例:爬取豆瓣Top250
入门第一个爬虫一般都是爬这个,实在是太简单.用了 requests 和 bs4 库. 1.检查网页元素,提取所需要的信息并保存.这个用 bs4 就可以,前面的文章中已经有详细的用法阐述. 2.找到下一 ...
- python爬虫-基础入门-爬取整个网站《3》
python爬虫-基础入门-爬取整个网站<3> 描述: 前两章粗略的讲述了python2.python3爬取整个网站,这章节简单的记录一下python2.python3的区别 python ...
- python爬虫-基础入门-爬取整个网站《2》
python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...
- python爬虫-基础入门-爬取整个网站《1》
python爬虫-基础入门-爬取整个网站<1> 描述: 使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数 ...
随机推荐
- cygwin64-安装包管理工具
1.dos command, install pkg $ setup-x86_64.exe -q -P curl $ setup-x86_64.exe -q -P lynx 2. cygwin64 c ...
- 第三次作业 史浩然 -assassin Talon
- Linux常用基本命令大全
1 一般情况下,自己安装linux系统都会选择简易版,这里面少很多命令,所以需要安装其他的包 yum install openssh-clients 安装scp的软件包 2 把当前一 ...
- 使用LR的socket协议进行进行性能测试,转解决方案
在用LR对公司delphi开发的C/S程序进行测试时,发现只有选择socket协议可以录制代码,经研究是通过TCP/IP的方式将参数保存在buffer中发送的方式来完成操作,但由于将buffer内容参 ...
- c# winform文本框数字,数值校验
文本框数字,数值校验 public void DigitCheck_KeyPress(object sender, KeyPressEventArgs e) { e.Handled = !char.I ...
- jquery cookie插件
jquery-cookie下载地址:http://www.bootcdn.cn/jquery-cookie/ 使用方法: 1.引入jQuery.Cookie.js插件. <script src= ...
- 解析Java对象的equals()和hashCode()的使用
解析Java对象的equals()和hashCode()的使用 前言 在Java语言中,equals()和hashCode()两个函数的使用是紧密配合的,你要是自己设计其中一个,就要设计另外一个.在多 ...
- java多线程--实现Runnable接口方式
因为java类只能继承一个类可以实现多个接口的特性,所以一般情况下不推荐使用继承Thread类实现多线程,下面是实现Runnable接口方式的简单多线程代码 package text; /** * 多 ...
- ssti记录
先看个python沙箱: #!/usr/bin/env python from __future__ import print_function print("Welcome to my P ...
- appium ios Demo
Appium Demo 录制图片,环境搭建完毕后根据视频基本能利用模拟器完成简单测试用例 感谢大神http://www.cnblogs.com/tobecrazy/p/4970188.html