【Python_requests学习笔记】requests模块中params参数用法
在Python的requests模块中,params参数主要用于通过URL的查询字符串发送数据。它可以是一个字典,列表或字符串。
- 字典形式:
import requests
params = {
'key1': 'value1',
'key2': 'value2'
}
response = requests.get('http://httpbin.org/get', params=params)
print(response.url)
输出结果:http://httpbin.org/get?key1=value1&key2=value2
- 列表形式:
import requests
params = [('key1', 'value1'), ('key1', 'value2'), ('key2', 'value3')]
response = requests.get('http://httpbin.org/get', params=params)
print(response.url)
输出结果:http://httpbin.org/get?key1=value1&key1=value2&key2=value3
- 字符串形式:
import requests
params = 'key1=value1&key2=value2'
response = requests.get('http://httpbin.org/get', params=params)
print(response.url)
输出结果:http://httpbin.org/get?key1=value1&key2=value2
注意:如果URL中已经包含查询参数,params中的参数会被追加到URL的查询字符串中。
评论已关闭