简体   繁体   中英

Python Beautiful Soup how to JSON decode to `dict`?

I'm new to BeautifulSoup in Python and I'm trying to extract dict from BeautifulSoup.

I've used BeautifulSoup to extract JSON and got beautifulsoup.beautifulsoup variable soup .

I'm trying to get values out of soup , but when I do result = soup.findAll("bill") I get an empty list [] . How can I extract soup to get dict result of:

{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 

=> result below

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"
}

UPDATE

Here is how I got 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)

You could remove BeautifulSoup :

import json
import urllib2

url = "https://www.govtrack.us/data/congress/113/votes/2013/s11/data.json"
data = json.load(urllib2.urlopen(url))

Not very familiar with BeautifulSoup but if you just need to decode JSON

import json

newDictionary=json.loads(str(soup))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM