爬虫之requests模块介绍
requests文档http://docs.python-requests.org/zh_CN/latest/index.html 【文档中包括的快速上手要精读,高级用法也要了解】
1.1 requests模块的作用:- 发送http请求,获取响应数据 【1.导入2.调用get方法对目标url发送请求】
pip/pip3 install requests
-
需求:通过requests向百度首页发送请求,获取该页面的源码
-
运行下面的代码,观察打印输出的结果
import requests
# 目标url
url = 'http://www.baidu.com'
# 向目标发送get请求
response = requests.get(url)
# 手动设置编码格式
response.encoding = 'utf8'
# 打印响应结果
print(response.text)
print(response.content)
print(response.content.decode())
运行效果: