Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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 - Fatal编程技术网

Python—如何将所有json打印到列表中

Python—如何将所有json打印到列表中,python,json,Python,Json,因此,我试图制作一个脚本,在一个网站上发出请求,然后比较是否有新的项目添加到请求中,这意味着如果有一个-提醒我,如果没有。在几分钟内请求相同的页面 因此,im haivng现在的问题是实际打印到一个列表中,它告诉我: TypeError: string indices must be integers 但每当我打印元素时,它只会给我一个“id”的打印,我不理解其原因- 代码看起来: resp = s.get(url) list = [] i = 0 for

因此,我试图制作一个脚本,在一个网站上发出请求,然后比较是否有新的项目添加到请求中,这意味着如果有一个-提醒我,如果没有。在几分钟内请求相同的页面

因此,im haivng现在的问题是实际打印到一个列表中,它告诉我:

TypeError: string indices must be integers
但每当我打印元素时,它只会给我一个“id”的打印,我不理解其原因-

代码看起来:

   resp = s.get(url)



    list = []
    i = 0
    for element in resp.json()['threads']['product']:
        print(element) #<--- this gives me a print of "id"
        list.append(element['fullTitle'])
    #ERROR - list.append(element['fullTitle'])
    #TypeError: string indices must be integers
        i+=1

    print(list)

    while True:
        try:
            new_list = []
            url = 'https://hellowebsite.com'
            resp = s.get(url)

            for element in resp.json()['threads']['product']:
                new_list.append(element['fullTitle'])

            print(new_list)

            for link in new_list:
                if link not in list:
                    print('New item found! - ' + link)


            else:
                print("No new itemfound!")
                time.sleep(10)

        except:
            randomtime = random.randint(6, 12)
有什么建议可以将所有的完整标题打印到一个列表中,然后继续进行比较


编辑完整跟踪

resp = s.get(url)

for element in resp.json()['threads']['product']['fullTitle']:
    print(element)
错误


你在重复错误的事情。这里的列表是“threads”,其中每个元素都有一个“product”字典,其中包含一个“fullTitle”元素。因此,您可以迭代:

for thread in resp.json()['threads']
    print(thread['product']['fullTitle'])

在dict中迭代总是只给出关键点。如果您想要所有内容,请使用
.items()
。我想我做不到-因为我可以看到,我在通过for循环打印“fullTitle”时遇到了困难。我不知道您想做什么
fullTitle
是字典本身的一个元素。如果您只是想这样,为什么要迭代?只要使用
resp.json()['threads']['product']['fullTitle']
@DanielRoseman我试过了,然后我得到了一个错误
TypeError:list索引必须是整数或片,而不是str
-问题是我想在json中找到所有的完整标题。你从哪里得到的?显示您正在使用的代码和完整的回溯。这就是答案!我可能整个思路都写错了,但你确实帮了我,我真的很感激!
Process Process-1:
Traceback (most recent call last):
  File "C:\Users\Username\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 249, in _bootstrap
    self.run()
  File "C:\Users\Usersname\AppData\Local\Programs\Python\Python36\lib\multiprocessing\process.py", line 93, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Test.py", line 84, in script
    for element in resp.json()['threads']['product']['fullTitle']:
TypeError: list indices must be integers or slices, not str
for thread in resp.json()['threads']
    print(thread['product']['fullTitle'])