python requests包的request()函数中的参数-params和data的区别介绍


Posted in Python onMay 05, 2020

如下所示:

import requests
 url='http://www.baidu.com'
#下面使用requests.request(method, url, **kwargs)
re=requests.request('GET',url)

python requests包的request()函数中的参数-params和data的区别介绍

经验证,可用。

我们试着传入一个字典,首先用params参数。

python requests包的request()函数中的参数-params和data的区别介绍

结果为:

python requests包的request()函数中的参数-params和data的区别介绍

亮点在url和args。

我们还用get方法,把dic这个字典传给data试试看。

python requests包的request()函数中的参数-params和data的区别介绍

亮点还是在args和url。惊喜地发现,dic这个字典没传进去。

这是因为:

python requests包的request()函数中的参数-params和data的区别介绍

params是用来发送查询字符串,而data是用来发送正文的。post方法和get方法的特性是:这两种参数post方法都可以用,get方法只能发查询字符串,不能发送正文。

接下来试试看post方法:

python requests包的request()函数中的参数-params和data的区别介绍

上面这是用data参数传字典的,亮点在form。

再试试用params参数传这个字典:

python requests包的request()函数中的参数-params和data的区别介绍

亮点在url和args。

补充知识:python_request_三个参数

requests.request(method,url,**kwargs)

method:请求方法,对应get/put/post/delete/head/patch/options

url: 模拟获取页面的url连接

**kwrags:控制访问的参数,共13个

kwargs(13个参数):

(一)params

params:字典或者字节序列,作为参数增加到url中

例子:

import requests
kv={“wd”:“你好”}#拼接的内容用字典储存
r=requests.request(“GET”,“http://www.baidu.com/s”,params=kv)
print(r.url)
print(r.text)

运行后拼接的效果:http://www.baidu.com/s?wd=你好

(二)data

data:字典、字节、或文件对象,作为request

例子:

import requests
kv={“key1”:“value1”,“key2”:“value2”}
r=requests.request(“POST”,“http://httpbin.org/post”,data=kv)
print(r.text)

运行结果:

{
“args”: {},
“data”: “”,
“files”: {},
“form”: {
“key1”: “value1”,
“key2”: “value2”
},
“headers”: {
“Accept”: “/”,
“Accept-Encoding”: “gzip, deflate”,
“Connection”: “close”,
“Content-Length”: “23”,
“Content-Type”: “application/x-www-form-urlencoded”,
“Host”: “httpbin.org”,
“User-Agent”: “python-requests/2.18.1”
},
“json”: null,
“origin”: “113.235.118.39”,
“url”: “http://httpbin.org/post”
}

(三)json

json:JSON格式的数据,作为request的内容

(四)header

header:字典,http定制头

例子:

import requests
hd={‘user-agent':“Chrome/10”}#改变浏览器模拟
r=requests.request(“post”,“http://www.baidu.com”,headers=hd

(五)cookies:

cookies:字典或CookieJar,request中的cookie

(六)auth

auth:元组,支持HTTP认证功能

(七)files:

files:字典类型,传输文件

(八)tiemout

timeout:设定时间

(九)proxies

proxies:字典类型,设定访问代理服务器,可以增加登录认证

以上这篇python requests包的request()函数中的参数-params和data的区别介绍就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
win7 下搭建sublime的python开发环境的配置方法
Jun 18 Python
python获得一个月有多少天的方法
Jun 04 Python
Python使用正则表达式获取网页中所需要的信息
Jan 29 Python
利用pyinstaller将py文件打包为exe的方法
May 14 Python
Python3 log10()函数简单用法
Feb 19 Python
详解Python 4.0 预计推出的新功能
Jul 26 Python
基于python实现蓝牙通信代码实例
Nov 19 Python
Python实现手机号自动判断男女性别(实例解析)
Dec 22 Python
使用Python文件读写,自定义分隔符(custom delimiter)
Jul 05 Python
对Pytorch 中的contiguous理解说明
Mar 03 Python
Python+Selenium实现读取网易邮箱验证码
Mar 13 Python
python中的sys模块和os模块
Mar 20 Python
关于Python解包知识点总结
May 05 #Python
python 使用事件对象asyncio.Event来同步协程的操作
May 04 #Python
在python里使用await关键字来等另外一个协程的实例
May 04 #Python
python 异步async库的使用说明
May 04 #Python
Python插件机制实现详解
May 04 #Python
python3+selenium获取页面加载的所有静态资源文件链接操作
May 04 #Python
解决IDEA 的 plugins 搜不到任何的插件问题
May 04 #Python
You might like
PHP学习之PHP表达式
2006/10/09 PHP
mysql5写入和读出乱码解决
2006/11/25 PHP
PHP中将网页导出为Word文档的代码
2012/05/25 PHP
详谈php ip2long 出现负数的原因及解决方法
2017/04/05 PHP
JavaScript 闭包深入理解(closure)
2009/05/27 Javascript
jquery操作select option 的代码小结
2011/06/21 Javascript
IE关闭时判断及AJAX注销案例学习
2013/02/18 Javascript
javascript中的onkeyup和onkeydown区别介绍
2013/04/28 Javascript
深入理解JavaScript系列(49):Function模式(上篇)
2015/03/04 Javascript
JS实现仿Windows经典风格的选项卡Tab切换代码
2015/10/20 Javascript
JS和jQuery使用submit方法无法提交表单的原因分析及解决办法
2016/05/17 Javascript
AngularJS自定义控件实例详解
2016/12/13 Javascript
ES6中Array.copyWithin()函数的用法实例详解
2017/09/16 Javascript
vue组件中使用props传递数据的实例详解
2018/04/08 Javascript
Vue CLI3 如何支持less的方法示例
2018/08/29 Javascript
vue中v-for通过动态绑定class实现触发效果
2018/12/06 Javascript
vue2.0实现的tab标签切换效果(内容可自定义)示例
2019/02/11 Javascript
jQuery 筛选器简单操作示例
2019/10/02 jQuery
Vue 实现从小到大的横向滑动效果详解
2019/10/16 Javascript
Python中使用PIL库实现图片高斯模糊实例
2015/02/08 Python
python关闭windows进程的方法
2015/04/18 Python
Numpy掩码式数组详解
2018/04/17 Python
python挖矿算力测试程序详解
2019/07/03 Python
python实现图像全景拼接
2020/03/27 Python
Django使用list对单个或者多个字段求values值实例
2020/03/31 Python
基于Python词云分析政府工作报告关键词
2020/06/02 Python
HTML5验证以及日期显示的实现详解
2013/07/05 HTML / CSS
加拿大时尚潮流大码女装购物网站:Addition Elle
2018/04/02 全球购物
世界领先的电子书网站:eBooks.com(在线购买小说、非小说和教科书)
2019/03/30 全球购物
小学生安全保证书
2014/02/01 职场文书
观看信仰心得体会
2014/09/04 职场文书
小学生自我评价100字(15篇)
2014/09/18 职场文书
2016年优秀共产党员先进事迹材料
2016/02/29 职场文书
vue+spring boot实现校验码功能
2021/05/27 Vue.js
解析Redis Cluster原理
2021/06/21 Redis
Java org.w3c.dom.Document 类方法引用报错
2021/08/07 Java/Android