目录
requests是一个很实用的Python HTTP客户端库,编写爬虫和测试服务器响应数据时经常会用到,Requests是Python语言的第三方的库,专门用于发送HTTP请求
pip install requests
import requests class RequestClass01(): def requestMethod(self): r = requests.get("http://www.baidu.com") print(r) rc = RequestClass01() rc.requestMethod()
1)新建 requestsDemo 目录

2)在 requestsDemo 目录下新建 requests0..py 并写入代码

3)运行看结果

payload = {‘key1’: ‘value1’, ‘key2’: ‘value2’, ‘key3’: None}
r = requests.get('http://www.baidu.com ', params=payload)
下面根据聚合数据来进行requests的使用

生肖配对

新闻头条

类似python中的表单提交
payload = {‘key1’: ‘value1’, ‘key2’: ‘value2’}
r = requests.post(“http://httpbin.org/post”, data=payload)
5.1 post传参的第一种方式
qq号测吉凶

5.2 post传参的第二种方式(不建议使用)
qq号测吉凶

6. requests响应
r.status_code 响应状态码

r.heards 响应头

r.cookies 响应cookies

r.text 响应文本

r. encoding 当前编码

r. content 以字节形式(二进制)返回

7. requests扩充
1:添加等待时间
requests.get(url,timeout=1) #超过等待时间则报错
2:添加请求头信息
requests.get(url,headers=headers) #设置请求头
3:添加文件
requests.post(url, files=files) #添加文件
文件传输
url = ‘http://httpbin.org/post’
files = {‘file’: open(‘report.xls’, ‘rb’)}
r = requests.post(url, files=files)