一、背景说明

http请求的难易对一门语言来说是很重要的而且是越来越重要,但对于python一是urllib一些写法不太符合人的思维习惯文档也相当难看,二是在python2.x和python3.x中写法还有差别。

实在是太难用,开始差点由于这个原因想放弃python,直到看urllib.request文档时看到下边这句话,认识了requests。总的而言requests配得上“HTTP for Humans”的口号。

1.1 适用版本

适用于python2.6、python2.7、python3.4及以上版本,参见官方说明。我这里使用的是当前最新的python3.7。

1.2 安装requests模块

pip install requests
# ubuntu类系统也可以直接用apt安装
# sudo apt-get install python-requests

二、使用requests模块完成各种操作

下边对于https的链接请求时会带上”verify=False“参数,因为默认Python会进行证书校验如果不是信任的证书会报错,带上”verify=False“指示不进行证书校验。

2.1 引用requests模块

import requests

2.2 get请求

import requests

url='https://www.baidu.com'
r = requests.get(url,verify=False)
print(r.status_code)

2.3 post请求

import requests

url='https://www.baidu.com'
data='username=ls&password=toor'
r = requests.post(url,data=data,verify=False)
print(r.status_code)

当前很多api是以json形式提交的,所以在使用post的时候我们可能想提交json数据。

提交json有两步:一是data要编码成json形式(python中的字典形式上和json一样但本质上不一样所以要编码),二是设置“Content-type”头的值为application/json(设置头部参见下面2.5,这里先用)

import json
import requests # 一定要设置Content-Type值为application/json
headers={}
headers['Content-Type']='application/json' url='https://www.baidu.com'
data={"username":"ls","password":"toor"}
# 一定要用json.dumps把data格式化成json
# r = requests.post(url,headers=headers,data=json.dumps(data),verify=False)
# 或者直接使用json参数代替data,此时requests会自动进行格式化和设置Content-Type头的工作
r = requests.post(url,json=data,verify=False)
print(r.status_code)

为了方便对比验证,另外再附curl post提交的方法:

 curl -H "Content-Type:application/json" -X POST --data '{"username": "ls","password":"toor"}' https://www.baidu.com/

2.4 使用代理

import requests

url='http://docs.python-requests.org/en/master/'
proxies={
'http':'127.0.0.1:8080',
'https':'127.0.0.1:8080'
}
r = requests.get(url,proxies=proxies)
print(r.status_code)

2.5 自定义header

import requests

url='http://docs.python-requests.org/en/master/'
headers={
'User-Agent':'self-defind-user-agent',
'Cookie':'name=self-define-cookies-in header'
}
r = requests.get(url,headers=headers)
print(r.status_code)

2.6 自定义Cookie

实验发现如果自定义header中定义了cookies那么此处设置的cookies不生效

import requests

url='http://docs.python-requests.org/en/master/'
cookies={'name1':'cookie1','name2':'cookies2'}
#cookies=dict(name1='cookie1',name2='cookies2')
r = requests.get(url,cookies=cookies)
print(r.status_code)

2.7 会话保执

经常很多请求只有在登录后才能进行,实现登录效果一般的做法是执行登录请求,然后从返回结果中提取sessionid放入自定义cookie中。

这种方法在requests中也行得通,但requests提供了更为简单的方法,直接使用request.Session类来请求即可,其保持登录的原理是保留之前请求中服务端通过set-cookie等设置的参数。

s = Session()
url='http://docs.python-requests.org/en/master/'
# 所有方法和直接使用requests时一样用即可
s.get(url)

参考:

http://docs.python-requests.org/en/master/(官方文档)

https://www.cnblogs.com/landhu/p/7048255.html

https://stackoverflow.com/questions/9733638/post-json-using-python-requests

python3使用requests模块完成get/post/代理/自定义header/自定义Cookie的更多相关文章

  1. Python3:Requests模块的异常值处理

    Python3:Requests模块的异常值处理 用Python的requests模块进行爬虫时,一个简单高效的模块就是requests模块,利用get()或者post()函数,发送请求. 但是在真正 ...

  2. python3 get/post/使用代理/自定义header/自定义Cookie

    说明:urllib发送http请求并不是很人性化,更推荐使用在urllib基础上封装的.python2和python3都兼容的requests模块,移步查看. 一.get请求 get请求就是在构造Re ...

  3. Python3之Requests模块详解

    # 导入 Request模块 # 若本机无自带Request模块,可自行下载或者使用pip进行安装 # python版本Python3 import requests import json #### ...

  4. [实战演练]python3使用requests模块爬取页面内容

    本文摘要: 1.安装pip 2.安装requests模块 3.安装beautifulsoup4 4.requests模块浅析 + 发送请求 + 传递URL参数 + 响应内容 + 获取网页编码 + 获取 ...

  5. (转)Python3之requests模块

    原文:https://www.cnblogs.com/wang-yc/p/5623711.html Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另 ...

  6. Python3之requests模块

    Python标准库中提供了:urllib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务. 发送G ...

  7. (四)requests模块的cookies和代理操作

    基于requests模块的cookie操作 引言:有些时候,我们在使用爬虫程序去爬取一些用户相关信息的数据(爬取某个人“人人网”个人主页数据)时,如果使用之前requests模块常规操作时,往往达不到 ...

  8. Python——python3的requests模块的导入

    前言 突然就要搞python,我这个心哦~ 版本 | python 3.7 步骤 配置环境变量 右击-->属性-->打开文件位置 进入到脚本目录: C:\Users\Administrat ...

  9. 03爬虫-requests模块基础(1)

    requests模块基础 什么是requests模块 requests模块是python中原生基于网络模拟浏览器发送请求模块.功能强大,用法简洁高效. 为什么要是用requests模块 用以前的url ...

随机推荐

  1. Python 爬虫常用的库

    一.常用库 1.requests 做请求的时候用到. requests.get("url") 2.selenium 自动化会用到. 3.lxml 4.beautifulsoup 5 ...

  2. c#四舍五入取整

    Math.Round(3.45, 0, MidpointRounding.AwayFromZero) 上取整或下取整 Math.Ceiling(3.1)=4; Math.Floor(3.9)=3;

  3. Python day1_Base1笔记

    1.helloworld print('helloword') 2.输入输出 a=input('Please input a value') print(a) 3.标识符 1.由字母数字下划线构成 2 ...

  4. C# 图片人脸识别

    此程序基于 虹软人脸识别进行的开发 前提条件从虹软官网下载获取ArcFace引擎应用开发包,及其对应的激活码(App_id, SDK_key)将获取到的开发包导入到您的应用中 App_id与SDK_k ...

  5. Linux性能测试工具-UnixBench

    ■下载路径: unixbench-5.1.2.tar.gz :http://soft.vpser.net/test/unixbench/ unixbench-5.1.3.tar.gz :http:// ...

  6. ZOJ 4053 Couleur

    4053 思路: 主席树 先分别求前缀和后缀的逆序数 然后要求某一段的逆序数,就可以根据前缀或着后缀根据容斥求出答案, 这样需要枚举这一段中的数,求之前或者之后有多少个比他大或比他小的数, 这个可以通 ...

  7. 在Spring Boot中使用 @ConfigurationProperties 注解, @EnableConfigurationProperties

    但 Spring Boot 提供了另一种方式 ,能够根据类型校验和管理application中的bean. 这里会介绍如何使用@ConfigurationProperties.继续使用mail做例子. ...

  8. windows下开启端口映射配置办法

    #1.添加一个端口映射 netsh interface portproxy add v4tov4 listenaddress=大网IP listenport=端口  connectaddress=要映 ...

  9. java异常:java.lang.NullPointerException

    /** * 功能:空指针异常测试 */ /* Object[] parameters1=null; if(parameters1.length>0&&parameters1!=n ...

  10. every day a practice —— morning(7)

    It is probably because Willow was the last link to her parents and a pastime that goes back to her o ...