目录:
返回顶部
1. requests模块介绍
1、 Python标准库中提供了:urllib、urllib2、httplib等模块以供Http请求,但是,它的 API 太渣了。
2、 Requests 是使用 Apache2 Licensed 许可证的 基于Python开发的HTTP 库,其在Python内置模块的基础上进行了高度的封装
3、 从而使得Pythoner进行网络请求时,变得美好了许多,使用Requests可以轻而易举的完成浏览器可有的任何操作
2. requests请求头和请求体
1、发送http请求包含请求头和请求体两部分,请求头和请求体使用”\r\n\r\n”分隔,请求头和请求头之间用一个
‘\r\n’,进行分隔,get请求只有请求头没有请求体,post请求同时有请求头和请求体
2、发送http请求时如果涉及cookies,cookies是放到请求头中,如果是回写cookie时是将cookie放到响应头中回写的cookie一般名字就叫(set-cookie)
3、如果有重定向
- 响应头中有一个location参数
3. requests模块常用方法
1、 pip install requests
2、 response = requests.get('http://www.baidu.com/ ') #获取指定url的网页内容
3、 response.text #获取文本文件
4、 response.content #获取字节类型
5、 response.encoding = ‘utf-8’ #指定获取的网页内容用utf-8编码
response.encoding = response.apparent_encoding #下载的页面是什么编码就用什么编码格式
6、 response.cookies #拿到cookies
response.cookies.get_dict() #拿到cookie字典样式
返回顶部
1、requests发送get请求的常用参数
import requests ret = requests.get('https://github.com/timeline.json') print(ret.url) # 打印访问的url print(ret.text) # 打印返回值