9

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

我使用 BeautifulSoup 提取 JSON 并获得beautifulsoup.beautifulsoupvariable 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"
}

更新

这是我得到的soup

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)
4

2 回答 2

23

不是很熟悉 BeautifulSoup 但如果你只需要解码 JSON

import json

newDictionary=json.loads(str(soup))
于 2013-11-11T21:07:05.727 回答
17

您可以删除BeautifulSoup

import json
import urllib2

url = "https://www.govtrack.us/data/congress/113/votes/2013/s11/data.json"
data = json.load(urllib2.urlopen(url))
于 2013-11-12T00:22:21.040 回答