python get请求参数处理
时间: 2023-06-12 14:04:36 浏览: 67
在 Python 中,我们可以使用 `requests` 模块来发送 GET 请求,并且可以通过在 URL 中添加查询字符串来传递参数。查询字符串是指 URL 中 `?` 后面的部分,例如:
```
http://example.com/path/to/resource?key1=value1&key2=value2
```
在这个 URL 中,`key1=value1` 和 `key2=value2` 就是查询字符串,它们用 `&` 符号连接起来。
要在 Python 中添加查询字符串,可以使用 `params` 参数。例如:
```python
import requests
url = 'http://example.com/path/to/resource'
params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get(url, params=params)
```
这里我们把参数传递给 `params` 参数,并且使用 `requests.get()` 发送 GET 请求。`requests` 模块会自动将参数添加到 URL 中,然后发送请求并返回响应对象。
如果你想要传递多个值到同一个键名中,可以使用一个列表:
```python
import requests
url = 'http://example.com/path/to/resource'
params = {'key': ['value1', 'value2']}
response = requests.get(url, params=params)
```
这样就会生成一个类似于 `http://example.com/path/to/resource?key=value1&key=value2` 的 URL,其中 `key` 的值是一个列表。
在获取响应后,你可以使用 `response.text` 属性来获取响应内容。如果你需要解析响应内容,可以使用 `response.json()` 方法将 JSON 数据转换为 Python 对象。