来自python cod的url请求

2024-05-15 12:14:01 发布

您现在位置:Python中文网/ 问答频道 /正文

import json
import urllib2    
response = urllib2.urlopen('http://www.energyhive.com/mobile_proxy/getCurrentValuesSummary?token=VtxgIC2UnhfUmXe_pBksov7-lguAQMZD')    
content = response.read()
print(content)

从我得到的代码

[{"cid":"PWER","data":[{"1437957635000":37}],"sid":"9271","units":"kWm","age":6},{"cid":"PWER_GAC","data":[{"1437957635000":0}],"sid":"9271","units":null,"age":6},{"cid":"FBAK_IMM","data":[{"1437957629000":0}],"sid":"9271","units":null,"age":12},{"cid":"PWER_IMM","data":[{"1437957629000":0}],"sid":"9271","units":null,"age":12}]

我没办法从普华永道获得数据


Tags: importjsonhttpagedataresponsecontenturllib2
2条回答

您只需将其作为JSON加载并转到正确的键:

for x in json.loads(content):
    if x["cid"] == "PWER_GAC":
        print(x["units"])
        print(x["age"])
        print(x["sid"])
        print(x["data"])
        print(x["cid"])

您正在接收的数据是一个字符串,您可以使用json.loads()将其转换为Python数据结构。然后遍历dict,直到cid与您要查找的内容匹配。你知道吗

content = json.loads(content)
for i in content:
    if(i['cid'] == 'PWER_GAC'):
        print(i)

相关问题 更多 >