使用requests模块完成自定义get/post/代理/cookie

一、背景说明
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 requestsurl='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)

原文来自:https://www.cnblogs.com/lsdb/p/9071015.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值