在Python中,发送GET请求通常使用`requests`库,以下是使用`requests`库发送GET请求的基本步骤和示例代码:
1. 导入`requests`库:
```python
import requests
2. 定义请求的URL:
```python
url = 'http://example.com/api/data'
```python
params = {
'param1': 'value1',
'param2': 'value2'
}
response = requests.get(url, params=params)
4. 处理响应,获取状态码、文本内容等:
```python
status_code = response.status_code
text = response.text
5. 打印响应内容:
```python
print(text)
完整的示例代码如下:
```python
import requests
定义请求的URL和参数
url = 'http://example.com/api/data'
params = {
'param1': 'value1',
'param2': 'value2'
}
发送GET请求
response = requests.get(url, params=params)
处理响应
status_code = response.status_code
text = response.text
打印响应内容
print(text)
如果需要使用`urllib`库发送GET请求,代码如下:
```python
import urllib.request
定义请求的URL
url = 'http://example.com/api/data'
发送GET请求
response = urllib.request.urlopen(url)
读取响应内容
html = response.read()
打印响应内容
print(html.decode('utf-8'))
请根据实际需要选择使用`requests`或`urllib`库发送GET请求