python requests 2 : GET 、 POST请求

一、GET请求

1、发送GET请求

输入:url

方法:requests.get(url)

import requests
r=requests.get('https://api.github.com/events')

输出:Response对象

2、发送包含Query String的GET 请求

输入:url, dict

方法:requests.get(url, params=dict)

输出:Response对象

二、POST请求

python requests 2 : GET 、 POST请求_weixin_xyyqwl的博客-优快云博客

 

1、使用dict发送包含表单的POST请求

输入:url, dict

方法:requests.post(url, data=dict)

输出:Response对象

 

  

2、使用tuple发送包含表单的POST请求

输入:url, tuple

方法:requests.post(url, data=tuple)

输出:Response对象

 3、dict转换成string ,发送包含json的POST请求

python requests 2 : GET 、 POST请求_weixin_xyyqwl的博客-优快云博客

输入:url, string

方法:

json.dumps(dict)

requests.post(url, data=string)

输出:Response对象

 

 4、直接使用dict,发送包含json的POST请求

输入:url, dict

方法:

requests.post(url, json=dict)

输出:Response对象

5、发送更改headers的POST请求

输入:url, dict,string

方法:requests.get(url, headers=dict,data=string)

输出:Response对象

例1:指定Content-Type为application/json

>>> import json
>>> payload={'key1':'value1'}
>>> jstr=json.dumps(payload)
>>> h1={'Content-Type': 'application/json'}
>>> type(jstr)
<class 'str'>
>>> type(h1)
<class 'dict'>
>>> r=requests.post("http://httpbin.org/post",headers=h1,data=jstr)
>>> print(r.text)
https://blog.youkuaiyun.com/weixin_51380973/article/details/123739184?spm=1001.2014.3001.5501

 例2:指定Content-Type为application/json

>>> import json
>>> payload={'key1':'value1'}
>>> jstr=json.dumps(payload)
>>> h1={'Content-Type': 'x-www-form-urlencoded;charset=UTF-8'}
>>> r=requests.post("http://httpbin.org/post",headers=h1,data=jstr)
>>> print(r.text)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值