Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从json中提取某些数据_Python_Json_Api_Extract - Fatal编程技术网

Python 从json中提取某些数据

Python 从json中提取某些数据,python,json,api,extract,Python,Json,Api,Extract,您好,我想从这个api中提取所有标题,看看标题中是否有任何更改。。如何使用python实现这一点 我得到这个错误: before_set = before['data']['items']['title'] TypeError: list indices must be integers or slices, not str 这是我的密码: import requests import json try: with open('notice.json', 'r') as curre

您好,我想从这个api中提取所有标题,看看标题中是否有任何更改。。如何使用python实现这一点

我得到这个错误:

before_set = before['data']['items']['title']
TypeError: list indices must be integers or slices, not str

这是我的密码:

import requests
import json

try:
    with open('notice.json', 'r') as current_notice:
        before = json.loads(current_notice.read())
except IOError:
    before = requests.get('https://www.huobi.com/p/api/contents/').json()
    with open('notice.json', 'w') as current_notice:
        current_notice.write(json.dumps(before))
    print("First run....")

after = requests.get('https://www.huobi.com/p/api/contents/').json()

before_set = before['data']['items']['title']
after_set = after['data']['items']['title']

new_set = after_set - before_set

while True:
    try:
        if not new_set:
            print("No change... Exiting.")
        if new_set:
            print("There are changes")
    except Exception as e:
        print(e)
        pass            

['data']['items']之前的
中包含的数据是一个列表。您可以使用以下方式访问第一个项目的标题:

before['data']['items'][0]['title']
要获取所有标题,可以使用列表:

before_set = [item['title'] for item in before['data']['items']]

['data']['items']
之前的
中包含的数据是一个列表。您可以使用以下方式访问第一个项目的标题:

before['data']['items'][0]['title']
要获取所有标题,可以使用列表:

before_set = [item['title'] for item in before['data']['items']]

这里的问题是响应['data']['items']以列表的形式出现

type(response['data']['items'])
<class 'list'>
类型(响应['data']['items'])

您可以使用列表理解来解决这个问题,或者尝试使用集合中的默认dict

这里的问题是响应['data']['items']以列表的形式出现

type(response['data']['items'])
<class 'list'>
类型(响应['data']['items'])

您可以使用列表理解来解决这个问题,或者尝试使用集合中的默认dict

这是因为['data']['items']
之前的
有多个项,因此它返回一个数组

您可以按如下方式迭代列表:

for item in before['data']['items']:
  print item['title']
也可以将所有标题保存到变量中,如:

titles = [item['title'] for item in before['data']['items']]

这是因为['data']['items']
之前的
有多个项,因此它返回一个数组

您可以按如下方式迭代列表:

for item in before['data']['items']:
  print item['title']
也可以将所有标题保存到变量中,如:

titles = [item['title'] for item in before['data']['items']]

为了获得最佳可视化效果,我建议您将数据与id进行比较,例如:

bitems = before.get('data', {}).get('items', [])
before_data = [(item.get('id'), item.get('title')) for item in bitems]
aitems = after.get('data', {}).get('items', [])
after_data = [(item.get('id'), item.get('title')) for item in aitems]

compare = set(before_data) ^ set(after_data)
print(compare)

为了获得最佳可视化效果,我建议您将数据与id进行比较,例如:

bitems = before.get('data', {}).get('items', [])
before_data = [(item.get('id'), item.get('title')) for item in bitems]
aitems = after.get('data', {}).get('items', [])
after_data = [(item.get('id'), item.get('title')) for item in aitems]

compare = set(before_data) ^ set(after_data)
print(compare)