Python发送get
时间: 2023-02-19 16:39:25 浏览: 57
使用 Python 发送 GET 请求可以使用内置的 urllib 库或第三方库 requests。
使用 urllib:
```python
import urllib.request
url = 'http://www.example.com'
response = urllib.request.urlopen(url)
html = response.read()
```
使用 requests:
```python
import requests
url = 'http://www.example.com'
response = requests.get(url)
html = response.content
```
注意:在使用 requests 时,如果返回的是 json 格式的数据,可以使用 `response.json()` 来获取返回的 json 数据。
相关问题
python发送get请求
你可以使用 Python 的 requests 库来发送 GET 请求。以下是一个简单的示例:
```python
import requests
# 发送 GET 请求并获取响应
response = requests.get('https://api.example.com/data')
# 检查响应状态码
if response.status_code == 200:
# 以文本格式获取响应内容
data = response.text
print(data)
else:
print('请求失败')
```
在上面的示例中,我们使用 requests.get() 方法发送了一个 GET 请求,并将响应保存在 response 变量中。你可以通过检查 response.status_code 来判断请求是否成功(200 表示成功)。然后,你可以通过 response.text 来获取响应的内容。
当然,你可以根据实际需求来设置请求的参数、请求头等。这只是一个简单的示例,供你参考。
python 发送get请求
你可以使用 Python 的 requests 库来发送 GET 请求。以下是一个示例代码:
```python
import requests
url = "http://example.com/api/data"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
print(data)
else:
print("Error:", response.status_code)
```
在这个示例中,我们使用 requests 库发送 GET 请求到 http://example.com/api/data。如果响应的状态码为 200,我们将响应的 JSON 数据输出到控制台。否则,我们输出错误代码。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)