本阶段主要学习requests这个http模块,该模块主要用于发送请求响应,该模块有很多的替代模块,比如说urllib模块,但是在工作中用的最多的还是requests模块,requests的代码简洁易懂,相对于臃肿的urllib模块,使用requests编写的爬虫代码将会更少,而且实现某一功能将会简单。因此建议大家掌握该模块的使用。整套爬虫笔记直接地址: 【python爬虫】80页md笔记0基础到scrapy项目高手,第(4)篇:requests和网络数据获取进阶 - 知乎共 8 章,37 子模块,总计近6w字接上一篇4.requests模块发送post请求
4.1 requests发送post请求的方法
4.2 POST请求练习下面面我们通过金山翻译的例子看看post请求如何使用: 1. 地址:<ht://fy.iciba.com/> 思路分析
2. 确定请求的参数
4. 模拟浏览器数据 4.2.3 抓包分析的结论
4.2.4 代码实现
import requestsimport jsonclass King(object): def __init__(self, word): self.url = 'http://fy.iciba.com/ajax.php?a=fy' self.word = word self.headers = { 'User-Agent': '' } self.post_data = { 'f': 'auto', 't': 'auto', 'w': self.word } def get_data(self): response = requests.post(self.url, headers=self.headers, data=self.post_data) # 默认返回bytes类型,除非确定外部调用使用str才进行解码操作 return response.content def parse_data(self, data): # 将json数据转换成python字典 dict_data = json.loads(data) # 从字典中抽取翻译结果 try: print(dict_data['content']['out']) except: print(dict_data['content']['word_mean'][0]) def run(self): # url # headers # post----data # 发送请求 data = self.get_data() # 解析 self.parse_data(data)if __name__ == '__main__': # king = King('人生苦短,及时行乐') king = King('China') king.run() # python标准库有很多有用的方法,每天看一个标准库的使用 知识点:掌握 requests模块发送post请求5.利用requests.session进行状态保持
5.1 requests.session的作用以及应用场景
5.2 requests.session使用方法
5.3 课堂测试
5.3.1 提示1. 对github登陆以及访问登陆后才能访问的页面的整个完成过程进行抓包2. 确定登陆请求的url地址、请求方法和所需的请求参数* 部分请求参数在别的url对应的响应内容中,可以使用re模块3. 确定登陆后才能访问的页面的的url地址和请求方法4. 利用requests.session完成代码 5.3.2 参考代码
知识点:掌握 利用requests.session进行状态保持数据提取概本阶段本文主要学习响应之后如何从响应中提取我们想要的数据,在本阶段本文中我们会讲解一些常用的方法和模块,基本上我们以后遇到的情况在掌握本阶段本文之后都能搞定 数据提取概述知识点
1.响应内容的分类
知识点:了解 响应内容的分类2.认识xml以及和html的区别
2.1 认识xml
<bookstore><book category='COOKING'> <title lang='en'>Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book><book category='CHILDREN'> <title lang='en'>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book><book category='WEB'> <title lang='en'>Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book></bookstore> 上 面的xml内容可以表示为下面的树结构: 2.2 xml和html的区别
知识点:了解 xml和html的区别2.3 常用数据解析方法数据提取-jsonpath模块知识点
1.jsonpath模块的使用场景
jsonpath 可以按照key对python字典进行批量数据提取 知识点:了解 jsonpath模块的使用场景2.jsonpath模块的使用方法2.1 jsonpath模块的安装
pip install jsonpath 2.2 jsonpath模块提取数据的方法from jsonpath import jsonpath ret = jsonpath(a, 'jsonpath语法规则字符串') 2.3 jsonpath语法规则2.4 jsonpath使用示例 book_dict = { 'store': { 'book': [ { 'category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95 }, { 'category': 'fiction', 'author': 'Evelyn Waugh', 'title': 'Sword of Honour', 'price': 12.99 }, { 'category': 'fiction', 'author': 'Herman Melville', 'title': 'Moby Dick', 'isbn': '0-553-21311-3', 'price': 8.99 }, { 'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99 } ], 'bicycle': { 'color': 'red', 'price': 19.95 } } } from jsonpath import jsonpath print(jsonpath(book_dict, '$..author')) # 如果取不到将返回False # 返回列表,如果取不到将返回False 3.jsonpath练习
参考代码: import requests import jsonpath import json
知识点:掌握 jsonpath模块的使用未完待续 下一期下一章完整笔记请看文章开头 |
|