Python之使用request发送请求

2020-08-25  本文已影响0人  lily_5945

Python3自带的http.client和urllib.request都能发送http请求,不过相对来说使用较麻烦,第三方库requests让发送请求更简单,支持自动编码解码,会话保持,长连等

requests安装

requests的使用

1,发送一个带参数的post请求
headers = {
    'content-type': 'application/x-www-form-urlencoded',
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'
}
URL = 'https://******123xue.cn/fs/uc/accessToken/create'
# requests 允许使用 params 关键字参数,以一个字符串字典来提供这些参数。
payload={
    'corpId': 'fa6aeddc25a848b4bd9e8b925d8c6182',
    'account': '******',
    'pwd': '******',
    'useMobile': False
 }
res = requests.post(url=URL, data=payload, headers=headers)
# 这里返回的是一个json字符串,所以转换成json格式,注意json后面带括号
res = res.json()

注:1,有些请求的headers可以不填,这里是业务需要。

2,发送put、delete、head、optiions请求也很简单
r = requests.put('http://yunweicai.com/put', data = {'key':'value'})

r = requests.delete('http://yunweicai.com/delete')

r = requests.head('http://yunweicai.com/get')

r = requests.options('http://yunweicai.com/get')

请求参数

响应解析

上一篇下一篇

猜你喜欢

热点阅读