• 全部
  • 互动交流
  • 文章分享

求助,请教如何把网页上json数据对应提取出来

lslcxlsl 西安德阳电子科技有限公司 硬件工程师  2020-11-23 03:20:11
{"category":["1层1号门","1层2号门","1层3号门","1层5号门","1层6号门","1层7号门","1层8号门","1层9号门"],"series_in":[1872,946,911,1705,285,31,51,136],"series_out":[557,262,1759,759,935,611,273,25]}


category是名称, series_in,和series_out 对应的是数据
这个数据我想一一只查找1层1号门,2号门,3号门这几个门的 series_in,和series_out 进出数据 ,
比如 显示为 :1层1号门 进1872,出557这样就可以了
...全文
202 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
欢乐的小猪 2020-11-25
data = str(urllib.request.urlopen(url, context=ctx).read()) 这个data是个字典,你这里是个字符串。需要转换为字典,可用json.loads
data_str='{"category":["1层1号门","1层2号门","1层3号门","1层5号门","1层6号门","1层7号门","1层8号门","1层9号门"],"series_in":[1872,946,911,1705,285,31,51,136],"series_out":[557,262,1759,759,935,611,273,25]}'
import json
data = json.loads(data_str)
ls=['1层1号门','2号门','3号门']
for cg in ls:
    if cg in data['category']:
        index=data['category'].index(cg)
        print("{} 进{}, 出{}".format(cg,data['series_in'][index],data['series_out'][index]))
回复
lslcxlsl 2020-11-24
引用 4 楼 欢乐的小猪 的回复:
这个不用怎么提取吧。直接访问就成了
data = {"category":["1层1号门","1层2号门","1层3号门","1层5号门","1层6号门","1层7号门","1层8号门","1层9号门"],"series_in":[1872,946,911,1705,285,31,51,136],"series_out":[557,262,1759,759,935,611,273,25]}
ls=['1层1号门','2号门','3号门']
for cg in ls:
    if cg in data['category']:
        index=data['category'].index(cg)
        print("{} 进{}, 出{}".format(cg,data['series_in'][index],data['series_out'][index]))
这个我执行了没有循环2 import urllib.request, urllib.parse, urllib.error import ssl # Ignore SSL certificate errors ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE # use urllib to achieve the web data (json) url = 'xxxxx' data = str(urllib.request.urlopen(url, context=ctx).read()) print(data) ls=['1层1号门','2号门','3号门'] for cg in ls: if cg in data['category']: index=data['category'].index(cg) print("{} 进{}, 出{}".format(cg,data['series_in'][index],data['series_out'][index])) 提示这个错误 if cg in data['category']: TypeError: string indices must be integers
回复
欢乐的小猪 2020-11-24
这个不用怎么提取吧。直接访问就成了
data = {"category":["1层1号门","1层2号门","1层3号门","1层5号门","1层6号门","1层7号门","1层8号门","1层9号门"],"series_in":[1872,946,911,1705,285,31,51,136],"series_out":[557,262,1759,759,935,611,273,25]}
ls=['1层1号门','2号门','3号门']
for cg in ls:
    if cg in data['category']:
        index=data['category'].index(cg)
        print("{} 进{}, 出{}".format(cg,data['series_in'][index],data['series_out'][index]))
回复
是香香本人 2020-11-24
import jsonpath
json_data = {"category":["1层1号门","1层2号门","1层3号门","1层5号门","1层6号门","1层7号门","1层8号门","1层9号门"],
             "series_in":[1872,946,911,1705,285,31,51,136],
             "series_out":[557,262,1759,759,935,611,273,25]}
print(type(json_data))
my_list=[]
for i in json_data:
    for x in range(3):
        key_value = jsonpath.jsonpath(json_data,f"$..{i}{[x]}")
        my_list.append(key_value)
# print(my_list)

for i in range(3):
    print(f"{my_list[i]}进{my_list[i+3]}出{my_list[i+6]}")
小白一个,方法不够简洁,还请大神指教,谢谢~
回复
考古学家lx 2020-11-23
假设你已经 json转换过了

item = {"category":["1层1号门","1层2号门","1层3号门","1层5号门","1层6号门","1层7号门","1层8号门","1层9号门"],"series_in":[1872,946,911,1705,285,31,51,136],"series_out":[557,262,1759,759,935,611,273,25]}
item2 = {}
for category,series_in,series_out in zip(item['category'],item['series_in'],item["series_out"]):
    item2[category] = category+'进'+str(series_in)+'出'+str(series_out)
print(item2)
回复
YungGuo 2020-11-23
import json
import re

s = '{"category":["1层1号门","1层2号门","1层3号门","1层5号门","1层6号门","1层7号门","1层8号门","1层9号门"],' \
    '"series_in":[1872,946,911,1705,285,31,51,136],"series_out":[557,262,1759,759,935,611,273,25]}'
s = json.loads(s)
category = s['category']
series_in = s['series_in']
series_out = s['series_out']
items = [{'door_number': category[i], 'enter': series_in[i], 'out': series_out[i]} for i in range(len(category))]
for item in items:
    number = re.findall(r'\w层(\w+)号门', item['door_number'])[0]
    if number == '1' or number == '2' or number == '3':
        print('{} 进{} 出{}'.format(item['door_number'], item['enter'], item['out']))
回复
相关推荐
发帖
脚本语言
创建于2007-08-27

3.7w+

社区成员

JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
申请成为版主
帖子事件
创建了帖子
2020-11-23 03:20
社区公告

CSDN 脚本语言社区接受专栏投稿(专栏会在顶部创建专属你的栏目),投稿需满足以下要求:

  • 脚本语言技术相关;
  • 文章持续更新,保持活跃;
  • 内容清晰明了,干货为主;
  • 文章排版有序,有条有理。

本社区开通招聘专栏,发布招聘信息请联系版主,发布者需要保证招聘信息真实有效,CSDN 平台和版主不对招聘内容负责!

联系方式:私聊版主、发送邮件、QQ联系等均可: