gpt4 book ai didi

Python Beautiful Soup 如何将 JSON 解码为 `dict` ?

转载 作者:行者123 更新时间:2023-12-01 21:52:12 24 4
gpt4 key购买 nike

我是 Python 中的 BeautifulSoup 新手,我正在尝试从 BeautifulSoup 中提取 dict

我使用 BeautifulSoup 提取 JSON 并获得 beautifulsoup.beautifulsoup 变量 soup

我试图从 soup 中获取值,但是当我执行 result = soup.findAll("bill") 时,我得到一个空列表 []。我怎样才能提取汤来获得dict结果:

{u'congress': 113,
u'number': 325,
u'title': u'A bill to ensure the complete and timely payment of the obligations of the United States Government until May 19, 2013, and for other purposes.',
u'type': u'hr'}


print type(soup)
print soup

=> 结果如下

BeautifulSoup.BeautifulSoup

{
"bill": {
"congress": 113,
"number": 325,
"title": "A bill to ensure the complete and timely payment of the obligations of the United States Government until May 19, 2013, and for other purposes.",
"type": "hr"
},
"category": "passage",
"chamber": "s"
}
<小时/>

更新

这是我如何得到:

from BeautifulSoup import BeautifulSoup

import urllib2
url = urllib2.urlopen("https://www.govtrack.us/data/congress/113/votes/2013/s11/data.json")
content = url.read()
soup = BeautifulSoup(content)

最佳答案

对 BeautifulSoup 不太熟悉,但如果你只需要解码 JSON

import json

newDictionary=json.loads(str(soup))

关于Python Beautiful Soup 如何将 JSON 解码为 `dict` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19915010/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com