python http requests使用
时间: 2024-02-02 09:08:40 浏览: 27
Python中的requests库是一个常用的HTTP请求库,可以方便地发送HTTP/1.1请求。以下是使用requests库发送HTTP请求的示例代码:
1. 发送GET请求
```python
import requests
response = requests.get('http://www.example.com')
print(response.text)
```
2. 发送POST请求
```python
import requests
data = {'key1': 'value1', 'key2': 'value2'}
response = requests.post('http://www.example.com', data=data)
print(response.text)
```
3. 发送带有请求头的请求
```python
import requests
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get('http://www.example.com', headers=headers)
print(response.text)
```
4. 发送带有参数的请求
```python
import requests
params = {'key1': 'value1', 'key2': 'value2'}
response = requests.get('http://www.example.com', params=params)
print(response.text)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)