求助,请教如何把网页上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这样就可以了
...全文
485 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
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]}")
小白一个,方法不够简洁,还请大神指教,谢谢~
  • 打赏
  • 举报
回复
假设你已经 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']))

37,717

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧