requests发送HTTP请求
requests库是一个流行的用于发送Http请求的Python第三方库, 其设计简洁高效可以完美替代默认的urllib。
使用pip安装requests:
pip install requests
引入模块:
import requests
发送GET请求:
response = requests.get(url)
在response对象中可以得到响应的相关信息。
>>> response = requests.get('http://www.cnblogs.com')
>>> response = requests.get('http://www.cnblogs.com/Finley/')
>>> >>> response.status_code
200
>>> response.text
<!DOCTYPE html>
<html>
...
>>> response.raw # 获得原始响应
<requests.packages.urllib3.response.HTTPResponse object at 0x10e3b3490>
>>> response.request # 获得请求对象
<PreparedRequest [GET]>
发送其它方法的请求:
response = requests.post(url)
response = requests.head(url)
response = requests.delete(url)
response = requests.put(url)
response = requests.options(url)
传递url参数:
response = requests.get(url, params={key:val})
查看已经编码的url: response.url
添加请求头:
response = requests.get(url, headers={key: val})
查看请求头和响应头:
>>> response.headers
{'Content-Encoding': 'gzip',
'Transfer-Encoding': 'chunked',
'Expires': 'Mon, 21 Nov 2016 09:01:00 GMT',
'Vary': 'Accept-Encoding',
'Last-Modified': 'Mon, 21 Nov 2016 09:00:50 GMT',
'Connection': 'keep-alive',
'X-UA-Compatible': 'IE=10',
'Cache-Control':
'private, max-age=10',
'Date': 'Mon, 21 Nov 2016 09:00:50 GMT',
'Content-Type': 'text/html; charset=utf-8'
}
>>> response.request.headers
{'Connection': 'keep-alive',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'User-Agent': 'python-requests/2.11.1'
}
模拟提交表单:
response = requests.post(url, data={key: val})
可以使用multipart-encoded上传文件:
files = {'file': open(path, 'rb')}
response = requests.post(url, files=files)
也可以设置文件名和请求头:
files = {
'file': (filename, open(path, 'rb')),
header_name: header_val
}
response = requests.post(url, files=files)
添加cookies:
response = requests.post(url, cookies={key: val})
查看cookies:
>>> response.cookies
<RequestsCookieJar[]>
requests只能提供阻塞IO, 使用timeout以秒为单位设置响应超时时间:
response = requests.post(url, timeout=0.2)
requests发送HTTP请求的更多相关文章
- Python+requests 发送简单请求--》获取响应状态--》获取请求响应数据
Python+requests 发送简单请求-->获取响应状态-->获取请求响应数据 1.环境:安装了Python和vscode编译器(Python自带的编译器也ok).fiddler抓包 ...
- Python常见问题 - python3 使用requests发送HTTPS请求报certificate verify failed 错误
当你使用 requests 发送HTTPS请求时 requests.get(url, parmas=parmas, headers=header, cookies=cookie) 出现了以下错误 HT ...
- python+pytest接口自动化(4)-requests发送get请求
python中用于请求http接口的有自带的urllib和第三方库requests,但 urllib 写法稍微有点繁琐,所以在进行接口自动化测试过程中,一般使用更为简洁且功能强大的 requests ...
- requests发送post请求的一些疑点
前言 在Python爬虫中,使用requests发送请求,访问指定网站,是常见的做法.一般是发送GET请求或者POST请求,对于GET请求没有什么好说的,而发送POST请求,有很多朋友不是很清楚,主要 ...
- 使用 requests 发送 POST 请求
POST请求也就是向服务器提交数据,通常我们用来提交表单数据: import requests postdata = { //定义表单数据 "username": "ab ...
- 12.Python使用requests发送post请求
1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...
- requests发送HTTPS请求(处理SSL证书验证)
1.SSL是什么,为什么发送HTTPS请求时需要证书验证? 1.1 SSL:安全套接字层.是为了解决HTTP协议是明文,避免传输的数据被窃取,篡改,劫持等. 1.2 TSL:Transport Lay ...
- Python使用requests发送post请求的三种方式
1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...
- 使用 requests 发送 GET 请求
基本用法: import requests req = requests.get("http://www.baidu.com/") //发起GET请求 print(req.text ...
随机推荐
- Address already in use的解决方法
当客户端保持着与服务器端的连接,这时服务器端断开,再开启服务器时会出现: Address already in usr. 可以用netstat -anp | more 可以看到客户端还保持着与服务器的 ...
- fastreport 如何 设置 richview 的 行高
richview中的行高改变有点特别.必须在AfterData 事件执行的时候才能修改: 也就是说,如果简单的放一个按钮,去发送消息给richView->RichEdit ,然后调用frxRep ...
- django中自定义标签和过滤器
想要实现自定义标签和过滤器需要进行准备工作: 准备(必需)工作: 1 在某个app下创建一个名为templatetags(必需,且包名不可变)的包.假设我们在名为polls的app下创建了一个tem ...
- Dictionary和IDictionary
Dictionary<string> s = new Dictionary<string>(); 这个是s是Dictionary类型.是个类 类型,实现了接口,提供了更多的方法 ...
- H5测试区别与PC端测试关注点
除了基本的业务逻辑功能测试之后,H5页面的测试,需要关注以下几点: 1. 通过H5网页(非手机的返回功能)的返回功能可以返回,不会出现无法返回的情况. 2. 横屏竖屏相互切换,能自适应,并且布局不 ...
- 使用sessionStorage、localStorage存储数组与对象(转)
http://my.oschina.net/crazymus/blog/371757 使用sessionStorage.localStorage存储数组与对象 发表于3个月前(2015-01-26 1 ...
- HDU 1540 Tunnel Warfare(线段树+区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=1540 题目大意:抗日战争期间进行地道战,存在n个村庄用地道连接,输入D表示破坏某个村庄(摧毁与其相连的地道, 包 ...
- mORMot使用基础
mORMot 名称来自Marmot,对,土拨鼠,俗称旱獭,是一种生活在高寒山区的动物.多数都在白天活动,喜群居,善掘土,所挖地道深达数米,内有铺草的居室,非常舒适.通常洞穴都会有两个以上的入口,以策安 ...
- 重写,重载,super,this,继承
重写:overwrite/override 子类根据需要对从基类继承来的方法进行重写. 重写方法必须与被重写方法有相同的方法名,参数列表和返回类型. 重写方法不能使用比被重写方法更严格的访问权限. 重 ...
- poj1166学习中
#include <iostream> #include <string.h> #include <cstdio> #include <cmath> u ...