zl程序教程

您现在的位置是:首页 >  后端

当前栏目

爬虫之requests模块介绍

爬虫模块 介绍 requests
2023-09-11 14:15:15 时间

爬虫之requests模块介绍

requests文档http://docs.python-requests.org/zh_CN/latest/index.html      【文档中包括的快速上手要精读,高级用法也要了解】

1.1 requests模块的作用:

  • 发送http请求,获取响应数据        【1.导入2.调用get方法对目标url发送请求】

1.2 requests模块是一个第三方模块,需要在你的python(虚拟)环境中额外安装

  • pip/pip3 install requests

1.3 requests模块发送get请求

  1. 需求:通过requests向百度首页发送请求,获取该页面的源码

  2. 运行下面的代码,观察打印输出的结果

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())

运行效果: