使用Python2.7 GET Onenet平台的数据

效果

代码

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     requests1
   Description :
   Author :       神秘藏宝室
   date:          2018-02-01
-------------------------------------------------
   Change Activity:
                   2018-02-01:
-------------------------------------------------
"""

import requests
import json
import sys
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
    reload(sys)
    sys.setdefaultencoding(defaultencoding)

#设备ID
DEVICEID = '3225187'
#数据流名称
SENSORID = 'TEST'
#APIKEY
APIKEY = 'R9xO5NZm6oVI4YBHvCPKEqtwYtMA'

url = 'http://api.heclouds.com/devices/%s/datastreams/%s'%(DEVICEID,SENSORID)
print url
headers = {
                "api-key":APIKEY,
                "Connection":"close"
           }
r = requests.get(url,headers=headers)

print r.headers
print '1',20 * '*'
print r.content
print '2',20 * '*'
print r.text
print '3',20 * '*'
print ('从Onenet获取的ID为%s获取的最新数据是:%d'
       % (json.loads(r.content)['data']['id'],
       json.loads(r.content)['data']['current_value']))

posted on 2018-02-01 23:26  神秘藏宝室  阅读(1160)  评论(0编辑  收藏  举报

 >>>转载请注明出处<<<