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&currentPage=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爬虫系列 - 初探:爬取旅游评论的更多相关文章

  1. Python爬虫系列之爬取美团美食板块商家数据(二)

    今天为大家重写一个美团美食板块小爬虫,说不定哪天做旅游攻略的时候也可以用下呢.废话不多说,让我们愉快地开始吧~ 开发工具 Python版本:3.6.4 相关模块: requests模块: argpar ...

  2. Python爬虫系列之爬取美团美食板块商家数据(一)

    主要思路 目的: 根据输入的城市名,爬取该城市美团美食板块所有商家的数据.数据包括: 店名.评分.评论数量.均价.地址, 并将这些数据存入Excel中. 最后尝试对爬取到的数据做一个简单的分析. 克服 ...

  3. python爬虫系列之爬取多页gif图像

                   python爬取多页gif图像 作者:vpoet mail:vpoet_sir@163.com #coding:utf-8 import urllib import ur ...

  4. 【转载】教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神

    原文:教你分分钟学会用python爬虫框架Scrapy爬取心目中的女神 本博文将带领你从入门到精通爬虫框架Scrapy,最终具备爬取任何网页的数据的能力.本文以校花网为例进行爬取,校花网:http:/ ...

  5. Python爬虫实例:爬取B站《工作细胞》短评——异步加载信息的爬取

    很多网页的信息都是通过异步加载的,本文就举例讨论下此类网页的抓取. <工作细胞>最近比较火,bilibili 上目前的短评已经有17000多条. 先看分析下页面 右边 li 标签中的就是短 ...

  6. Python爬虫实例:爬取猫眼电影——破解字体反爬

    字体反爬 字体反爬也就是自定义字体反爬,通过调用自定义的字体文件来渲染网页中的文字,而网页中的文字不再是文字,而是相应的字体编码,通过复制或者简单的采集是无法采集到编码后的文字内容的. 现在貌似不少网 ...

  7. Python爬虫实例:爬取豆瓣Top250

    入门第一个爬虫一般都是爬这个,实在是太简单.用了 requests 和 bs4 库. 1.检查网页元素,提取所需要的信息并保存.这个用 bs4 就可以,前面的文章中已经有详细的用法阐述. 2.找到下一 ...

  8. python爬虫-基础入门-爬取整个网站《3》

    python爬虫-基础入门-爬取整个网站<3> 描述: 前两章粗略的讲述了python2.python3爬取整个网站,这章节简单的记录一下python2.python3的区别 python ...

  9. python爬虫-基础入门-爬取整个网站《2》

    python爬虫-基础入门-爬取整个网站<2> 描述: 开场白已在<python爬虫-基础入门-爬取整个网站<1>>中描述过了,这里不在描述,只附上 python3 ...

  10. python爬虫-基础入门-爬取整个网站《1》

    python爬虫-基础入门-爬取整个网站<1> 描述: 使用环境:python2.7.15 ,开发工具:pycharm,现爬取一个网站页面(http://www.baidu.com)所有数 ...

随机推荐

  1. 【Leetcode】【Medium】Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", ...

  2. iOS设计模式 - 桥接

    iOS设计模式 - 桥接 示意图 说明 1. 桥接模式为把抽象层次结构从实现中分离出来,使其可以独立变更,抽象层定义了供客户端使用的上层抽象接口,实现层次结构定义了供抽象层次使用的底层接口,实现类的引 ...

  3. Connection to linux server with ORACLE SQL DEVELOPER

    1.Link name is random 2.username and password is database account 3.host name  is ip address  ifconf ...

  4. Windows Server 2012 AD域管理创建

    前言 关于AD域管理及其权限划分概论: 1. AD域源于微软,适用于windows,为企业集中化管理和信息安全提供强力保障. 2. 提供域中文件夹共享,但同时又对不同用户有不用的权限. 3.通过对设备 ...

  5. [EffectiveC++]item3:尽可能使用const

    将某些东西声明为const可以帮助编译器检测出错误用法,const 编译器强制实施bitwise constness,但是你code的时候应该使用“概念上的常量性” 当const和non-const成 ...

  6. Linux命令--压缩解压(简化版)

    Linux tar.gz.tar.bz2.zip 等解压缩.压缩命令详解(简化版) Linux 常用的压缩与解压缩命令有:tar.gzip.gunzip.bzip2.bunzip2.compress ...

  7. Dictionary<Tkey.TValue>与SortedList

    一.概述 表示Key/Value集合,可以添加删除元素,允许按Key来访问元素.是Hashtable的泛型等效类. 它需要一个相等实现来确定键是否相等,可以使用实现了IEqualityComparer ...

  8. 程序人生:01如何做到招聘要求中的“要有扎实的Java基础”

    本文摘自左潇龙博客,原文出处:http://www.zuoxiaolong.com/html/article_232.html 来历 本文来自于一次和群里猿友的交流,具体的情况且听LZ慢慢道来. 一日 ...

  9. 使用loader打包静态文件-样式2

    这篇我们了解下css-loader常用的配置项,要配置的话,use里面就不再是一个字符串了 // 打包模块不知道该怎么办,就去模块配置里面该怎么办 module: { // 规则 rules: [{ ...

  10. 查看WIFI连接的信号强度

    netsh wlan show interface (netsh wlan show interface) -match '^\s+Signal' -replace '\s+Signal\s+:\s+ ...