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. 32、shiro 框架入门三

    1.AuthenticationStrategy实现 //在所有Realm验证之前调用 AuthenticationInfo beforeAllAttempts( Collection<? ex ...

  2. 比较一下Linux下的Epoll模型和select模型的区别

    一. select 模型(apache的常用) 1. 最大并发数限制,因为一个进程所打开的 FD (文件描述符)是有限制的,由 FD_SETSIZE 设置,默认值是 1024/2048 ,因此 Sel ...

  3. xcode5-ios7-如何添加120x120、152x152、76x76图标

    以120x120为例: 添加Icon_120x120.png-->.plist添加Icon files-->App Icons自动变化 1. 2. 3. ================= ...

  4. CSS3常用选择器(二)

    本文继续介绍css3新增的选择器. 1.选择器 first-child.last-child.nth-child 和 nth-last-child 利用这几个选择器能够针对一个父元素中的第一个子元素. ...

  5. JS --正则表达式验证、实战之邮箱模式

     JS验证格式:提高用户体验,验证文本. 需要防止程序员的代码结构更改攻击,因为web段的代码有可能会被更改,更改后JS有可能会验证不住那么,C#端在JS段通过验证的情况下,还需要进行二次验证 < ...

  6. hdu 2896 字典树解法

    #include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> ...

  7. 线程死锁情况和while在线程的作用

    public class printDemo04 { public static void main(String[] args) { Resource01 resource01 = new Reso ...

  8. H5-杂七杂八的标签

    1.overflow:设置当内容超出父级的宽高尺寸设置时的处理方式 a.hidden:隐藏超出部分的内容 b.auto:如果内容没有超出,就正常显示,如果超出,就隐藏内容并提供滚动条,可以滚动显示超出 ...

  9. sessionFactory

    SessionFactory接口:SessionFactory接口负责初始化Hibernate.它充当数据存储源的代理,并负责创建Session对象.这里用到了工厂模式.需要注意的是SessionFa ...

  10. spring访问静态资源文件

    用 Spring MVC 开发应用程序,对于初学者有一个很头疼的问题,那就是程序数据都已经查询出来了,但界面样式仍然十分丑陋,加载不了 css,js,图片等资源文件.当你在浏览器上直接输入某个css文 ...