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请求的更多相关文章

  1. Python+requests 发送简单请求--》获取响应状态--》获取请求响应数据

    Python+requests 发送简单请求-->获取响应状态-->获取请求响应数据 1.环境:安装了Python和vscode编译器(Python自带的编译器也ok).fiddler抓包 ...

  2. Python常见问题 - python3 使用requests发送HTTPS请求报certificate verify failed 错误

    当你使用 requests 发送HTTPS请求时 requests.get(url, parmas=parmas, headers=header, cookies=cookie) 出现了以下错误 HT ...

  3. python+pytest接口自动化(4)-requests发送get请求

    python中用于请求http接口的有自带的urllib和第三方库requests,但 urllib 写法稍微有点繁琐,所以在进行接口自动化测试过程中,一般使用更为简洁且功能强大的 requests ...

  4. requests发送post请求的一些疑点

    前言 在Python爬虫中,使用requests发送请求,访问指定网站,是常见的做法.一般是发送GET请求或者POST请求,对于GET请求没有什么好说的,而发送POST请求,有很多朋友不是很清楚,主要 ...

  5. 使用 requests 发送 POST 请求

    POST请求也就是向服务器提交数据,通常我们用来提交表单数据: import requests postdata = { //定义表单数据 "username": "ab ...

  6. 12.Python使用requests发送post请求

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  7. requests发送HTTPS请求(处理SSL证书验证)

    1.SSL是什么,为什么发送HTTPS请求时需要证书验证? 1.1 SSL:安全套接字层.是为了解决HTTP协议是明文,避免传输的数据被窃取,篡改,劫持等. 1.2 TSL:Transport Lay ...

  8. Python使用requests发送post请求的三种方式

    1.我们使用postman进行接口测试的时候,发现POST请求方式的编码有3种,具体的编码方式如下: A:application/x-www-form-urlencoded ==最常见的post提交数 ...

  9. 使用 requests 发送 GET 请求

    基本用法: import requests req = requests.get("http://www.baidu.com/") //发起GET请求 print(req.text ...

随机推荐

  1. IMoniker接口的MKParseDisplayName和 GetDisplayName的注意事项

    IMoniker接口的MKParseDisplayName和 GetDisplayName的注意事项  

  2. Docker私有仓库 Registry中的镜像管理

    这里主要介绍Registry v2的版本 查看Registry仓库中现有的镜像: # curl -XGET http://10.0.30.6:5000/v2/_catalog# curl -XGET ...

  3. TinyXML:一个优秀的C++ XML解析器

    //-------------------------------------------------------------------------------------------------- ...

  4. (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO

    . . . . . 目录 (一) 一起学 Unix 环境高级编程 (APUE) 之 标准IO (二) 一起学 Unix 环境高级编程 (APUE) 之 文件 IO (三) 一起学 Unix 环境高级编 ...

  5. Linux网络编程-SIGPIPE信号导致的程序退出问题

    当客户端close关闭连接时,若server端接着发送数据,根据TCP协议的规定,server端会收到RST响应,当server端再次往客户端发送数据时,系统会发出一个SIGPIPE信号给server ...

  6. 高频交易策略[z]

    Market Order以最高速下市价单(market order)是买方最基本的策略 Looking for Price Discrepancies 这个就是高频统计套利(high frequenc ...

  7. Oracle游标带参数

    Oracle游标是可以带参数的,而SqlServer的游标就不可以了 create or replace procedure a as cursor b(c_id int)is select * fr ...

  8. 剑指offer题目61-67

    面试题61:把二叉树打印成多行 public class Solution { public ArrayList<ArrayList<Integer> > Print(Tree ...

  9. 边表+SPFA

    传说中效率很NB的单元最短路径算法,传说中时间复杂度为O(kE),k为长度,平均值为2,不知道这话是谁说的,一说流传oi界几年了 边表就是数组模拟邻接表,没学会很难,学会很简单的样子啊 #includ ...

  10. 代码中修改组件的margin属性

    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_ ...