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. jquery remove/add css

    <input type="submit" class="btn btn-primary" id="submit" value=&quo ...

  2. LApacheMP基础环境搭建

    一.安装前准备 1.下载所需软件包: apr | http://apache.etoak.com/apr/ apr-util | http://apache.etoak.com/apr/ autoco ...

  3. anomaly detection algorithm

    anomaly detection algorithm 以上就是异常监测算法流程

  4. iOS响应者链和事件传递机制

    原文来自:http://www.cnblogs.com/zhw511006/p/3517248.html 响应者链(Responder Chain) 通常,一个iOS应用中,在一块屏幕上通常有很多的U ...

  5. asp.net GridView控件的列属性

    BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...

  6. Oracle 10g -- 修改DB的编码

    修改DB的原因是:因为我的DB不支持中文,所以每当我向数据库表中插入一条数据的时候,中文就都变了类似于“?(是反问号)”的乱码,为了能顺利插入成功,故做了此次修改; 系统:windows XP 英文版 ...

  7. AngularJS学习--- AngularJS中的模板template和迭代器过滤filter step2 step3

    1.AngularJS 模板---step2: mvc(Model-View-Controller)模式在后端用的比较多,在前端也是一样的常用; 在AngularJS中,一个视图是模型通过HTML模板 ...

  8. knockout.js $index 做列表索引小技巧

    我们都知道,在foreach binding中,使用$index可以得到基于0的索引序号,但在列表显示中,我们更希望这个索引是从1开始的,怎么处理呢? 这里,有个小技巧:使用$index() + 1, ...

  9. TNetHTTPClient演示

    TNetHTTPClient演示 TNetHTTPClient是DELPHI新增加的异步HTTP通信控件(区别于INDY的阻塞控件). unit Unit1; interface uses Winap ...

  10. Dedecms 首页调用副栏目内容方法

    最近一段时间一直打算修改英国移民网的首页,对首页的栏目内容也打算进行一系列的调用设置,可是在调用的时候出现了一些问题:调用不了英国移民网下的“移民快讯”这一副栏目下的内容!于是只能上网求助,在广大的网 ...