在python中从JSON提取数据

2024-05-14 21:14:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我可以从下面的Json数据访问next内的链接吗?我是这样做的

data = json.loads(html.decode('utf-8'))
for i in data['comments']:
        for h in i['paging']:
            print(h)
{

因为comments是一个主对象。在comments中有三个子对象datapagingsummary。上面的代码也是这样做的,在comments内部,因为paging是多个其他对象的对象,在循环和打印中。它给出了错误

for h in i['paging']: TypeError: string indices must be integers

    "comments": {
        "data": [
            {
                "created_time": "2016-05-22T14:57:04+0000",
                "from": {
                    "id": "908005352638687",
                    "name": "Marianela Ferrer"
                },
                "id": "101536081674319615443",
                "message": "I love the way you talk! I can not get enough of your voice. I'm going to buy Seveneves! I'm going to read it this week. Thanks again se\u00f1or Gates. I hope you have a beautiful day:-)"
            }
        ],
        "paging": {
            "cursors": {
                "after": "NzQ0",
                "before": "NzQ0"
            },
            "next": "https://graph.facebook.com/v2.7/10153608167431961/comments?access_token=xECxPrxuXbaRqcippFcrwZDZD&summary=true&limit=1&after=NzQ0"
        },
        "summary": {
            "can_comment": true,
            "order": "ranked",
            "total_count": 744
        }
    },
    "id": "10153608167431961"
}

Tags: to对象inyouidfordatasummary
1条回答
网友
1楼 · 发布于 2024-05-14 21:14:52

您在“comments”中进行迭代,结果得到三个对象:datapagingsummary。您只需要paging,但是您的第一个for循环希望您遍历所有其他的for循环。你知道吗

因此,当它以data开头时,您试图调用data['paging'],但这不起作用,因为data的值是一个列表,而不是一个字典。你知道吗

要立即访问paging

print data['comments']['paging']['next']

相关问题 更多 >

    热门问题