python爬取自定义网页数据后转换为dict
制作了一个软件,会不定时的更新,为提醒到用户,使用python爬取自己服务器上的一个文件,将里面的数据转换位为dict
- 文件内容
{
"0X01":
{
"ID" : 0X01,
"P_V" : 0X30,
"S_V" : 0X30
},
"0X02":
{
"ID" : 0X02,
"P_V" : 0X10,
"S_V" : 0X10
}
}
- 程序代码
from urllib import request
def HID_WEB_GET_VERSION(url = "https://xxx"): # 文件所在位置
content = ''
try:
wp = request.urlopen(url) #打开连接
content = str(wp.read(), encoding = "utf-8") #获取页面内容
dat = eval(content)
if dat:
return dat
except:
return 'ERROR'
if __name__ == '__main__':
dat = HID_WEB_GET_VERSION()
if dat=='ERROR':
print("错误!")
else:
print(dat['0X01']['P_V'])
print('%#x'%dat['0X01']['P_V'])