python requests params
时间: 2023-09-13 19:08:58 浏览: 144
参数和data有什么区别?
在python requests库的request()函数中,params和data都是可选参数,用于向服务器发送请求时传递参数的。区别如下:
params参数用于GET请求,将参数以键值对的形式添加到URL的末尾。例如,使用params参数可以向百度搜索发送一个GET请求,搜索关键词为'python',示例代码如下:
```
import requests
r = requests.get(url='http://dict.baidu.com/s', params={'wd': 'python'})
```
data参数用于POST请求,将参数以字典的形式作为请求的主体发送到服务器。例如,向百度贴吧发送一个POST请求,搜索关键词为'python',并且显示结果的页数为2,示例代码如下:
```
import requests
from fake_useragent import UserAgent
url = 'http://tieba.baidu.com/f?' # 基准url地址
data = {"kw": "python", "pn": 50} # data参数
headers = {'User-Agent': UserAgent().random} # headers参数
html = requests.post(url=url, data=data, headers=headers, verify=False).text # 发送POST请求
print(html) # 打印响应内容
```
总结来说,params参数适用于GET请求,将参数添加到URL的末尾;data参数适用于POST请求,将参数作为请求的主体发送到服务器。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Python requests模块详解](https://blog.csdn.net/lx1315998513/article/details/105746615)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *2* [【Python_requests学习笔记(三)】requests模块中params参数用法](https://blog.csdn.net/sallyyellow/article/details/129686842)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
- *3* [python requests包的request()函数中的参数-params和data的区别介绍](https://download.csdn.net/download/weixin_38500572/12852315)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"]
[ .reference_list ]
阅读全文