在Python中获取页面的JSON串通常需要使用`requests`库来获取网页内容,然后使用`json`模块解析返回的JSON格式数据。以下是一个简单的步骤说明:
1. 安装`requests`库(如果尚未安装):
pip install requests
2. 使用`requests`库发送HTTP请求获取网页内容:
import requests
发送GET请求
response = requests.get('http://example.com/api/data')
检查请求是否成功
if response.status_code == 200:
获取JSON字符串
json_str = response.text
else:
print(f"请求失败,状态码:{response.status_code}")
3. 使用`json`模块解析JSON字符串:
import json
解析JSON字符串
data = json.loads(json_str)
打印解析后的数据
print(data)
以上代码示例展示了如何从指定的URL获取JSON数据并解析它。请确保将`http://example.com/api/data`替换为您要请求的实际网址。