python post 参数_5分钟入门一个python库——requests

5分钟入门一个python库——requests

385debab2847d443f5d85dcb68451eb7.pngHello, 大家好,今天小 Geek 给大家介绍的是一个第三方库 <requests[1]>,什么来历呢?我们先来看看它有哪些 tag:

全球下载量超3亿次 python必备库爬虫利器 接口测试必会 For Human Beings

而它的作者就是下面这位颜值担当了— Kenneth Reitz

640?wx_fmt=png

最基础的用法(官方文档[2])

不管怎么样,我们先来安装它,毕竟它还不是 python 标准库的一份子呢

pip install requests

使用 Requests 发送网络请求

import requests
response = requests.get('http://httpbin.org/get')

通过 requests.get 方法,入参传入一个 URL,就可以获取一个 response 对象,如果你想看看网络请求返回的 response 对象是什么内容的话,可以使用 response.content 或 response.text 属性查看

print(response.text)
{
"args": {},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
"Host": "httpbin.org",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.97 Safari/537.36"
},
"origin": "219.133.101.172, 219.133.101.172",
"url": "https://httpbin.org/get"
}

好了,是时候正式上路了

接下来给大家介绍下如何应对 HTTP 请求的各种方法,请系好安全带?

GET 请求

其实在上面的基础用法中已经展示了 GET 请求怎么写,只是没有带参数,那么我们再来看看带参数的 GET 请求怎么玩

GET 示例:http://jsonplaceholder.typicode.com/posts?userId=1&id=2
params = {
   "userId": 1,
   "id": 2
 }
 url = "http://jsonplaceholder.typicode.com/posts"
 response = requests.get(url, params=params)

GET 请求的参数就是 ? 后面以 & 连接的键值对,你只需要把一个字典对象传给 requests.get 方法中的 params 参数即可, requests 库会默默帮你将字典对象进行 URL encode ,然后拼接成完整的 URL

POST 请求

POST 请求也是 HTTP 中最常用的请求方式之一,这里将介绍两种类型的 POST Body 体,分别是:application/x-www-form-urlencodedapplication/json(继续往下看哦,开始画重点)

POST 示例:http://httpbin.org/post
# Content-Type: application/x-www-form-urlencoded
data = {
   "userId": 1,
   "id": 2
 }
 url = "http://httpbin.org/post"
 response = requests.post(url, data=data)

注意,这里跟 GET 请求参数名是不一样的,在 POST 请求中传参给data 参数,requests 默认会把字典对象参数序列化为字符串参数,如上面的 data 参数会转换为 userId=1&id=2,聪明的同学可能发现了,这不是跟 GET 请求一样?

还是不一样的,俗话说得好,看事不要光看外表对吧。POST 参数转换后会放在 Body 体,且在 request headers 中要求注明 Content-Type,当然这个步骤 requests 库也会默默帮你做的,否则都对不起 For Human Beings

POST 第二种 Body 体类型 application/json, 这也是目前 HTTP 接口最流行的一种 Body 数据类型

# Content-Type: application/json
data = {
   "userId": 1,
   "id": 2
 }
 url = "http://httpbin.org/post"
 # 注意这里参数名不是 data 了 ,对,json, 相当易懂
 response = requests.post(url, json=data)

此时我们传入的 data 字典对象会被转换为 JSON 字符串放到 POST Body 体中,同时 request headers 中会添加Content-Type: application/json

POST 请求常用的 Body 类型还有一种叫 multipart/form-data,  主要用来传输长字节的类容(如文件),想了解更多的朋友看这:传送门[3]

PUT, DELETE, PATCH 请求

这几个都是 POST 的多胞胎兄弟, requests 库中把方法名改成对应的名称就好了,举个栗子:

response = requests.put(url, json=data)

response 对象是什么东东

前面我们讲了如何请求及请求的参数类型,那请求后我们怎么理解得到的 response 对象呢?

response 对象包含了请求信息和服务端返回的信息,其中常用到的属性有以下:

response.text 得到解码字符串,requests 根据响应内容自动解码

>response.text
u'[{"repository":{"open_issues":0,"url":"https://github.com/...'

response.content 得到字节类型响应体内容

>response.content
b'[{"repository":{"open_issues":0,"url":"https://github.com/...

response.json() 如果响应是  JSON 字符串,该方法得到反序列化对象,如列表、字典

>response.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...

我想使用代理

爬虫?你懂的

proxies = {
    "http": "http://127.0.0.1:8888",
    "https": "https://127.0.0.1:8888",
}
# 请求中设置代理(proxies)
response = requests.get("https://www.taobao.com", proxies=proxies)

The end, See you!

参考资料

[1]

requests: https://github.com/psf/requests

[2]

官方文档: https://cn.python-requests.org/zh_CN/latest/

[3]

传送门: https://cn.python-requests.org/zh_CN/latest/user/quickstart.html#post-multipart-encoded

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值