.(20.0分)编写一个Python程序,使用requests库发送GET请求到指定的URL(如“https://httpbin.org/get"),并打印出响应的状态码和响应内容。
时间: 2024-10-17 12:04:07 浏览: 58
在Python中,你可以使用`requests`库来发送HTTP GET请求。以下是一个简单的示例程序:
```python
import requests
# 定义目标URL
url = "https://httpbin.org/get"
# 使用requests.get()函数发送GET请求
response = requests.get(url)
# 检查状态码
if response.status_code == 200: # 状态码200表示成功
print(f"响应状态码: {response.status_code}")
else:
print(f"请求失败,状态码: {response.status_code}")
# 打印响应内容
print("响应内容:")
print(response.text)
```
在这个程序中,我们首先导入`requests`库,然后指定要请求的URL。使用`requests.get()`方法发送GET请求,并将返回的对象存储在`response`变量中。接着检查响应的状态码,如果状态码为200,说明请求成功,我们会打印状态码。最后,我们打印出响应的内容。
阅读全文
相关推荐


















