python requests 例子
时间: 2023-11-09 13:02:04 浏览: 25
好的,以下是一个使用 Python requests 库发送 GET 请求的例子:
```python
import requests
response = requests.get('https://www.baidu.com')
print(response.text)
```
这个例子会向百度首页发送一个 GET 请求,并打印出响应内容。
相关问题
python requests 返回
以下是两种Python使用requests库发送请求并获取响应的例子:
1. 发送GET请求并获取响应内容:
```python
import requests
url = 'https://www.httpbin.org/get'
response = requests.get(url=url)
print(response.text)
```
2. 发送带参数的GET请求并获取响应内容:
```python
import requests
url = 'https://www.httpbin.org/get'
params = {'name': 'Lisa', 'age': 20}
response = requests.get(url=url, params=params)
print(response.text)
```
python requests pn
在使用Python的requests库发送请求时,可以通过params参数来传递查询参数。在你提供的代码中,params参数用于指定查询参数,其中"pn"表示当前页数。通过将params参数传递给requests.get()方法,可以将查询参数拼接到URL中,以获取指定页数的内容。
举个例子,如果想要获取百度贴吧中"python"吧的第2页内容,可以使用params参数将"kw"设为"python","pn"设为50。然后将params参数传递给requests.get()方法,获取响应内容。
请注意,params参数的数据类型为字典类型,其中键为查询参数的名称,值为查询参数的值。在发送GET请求时,params参数会自动将字典编码并拼接到URL中,以便将查询参数传递给服务器。这样,你就可以轻松地获取指定页数的内容了。