Python Belle Soupe comment JSON décoder pour `dict`?
Je suis nouveau sur BeautifulSoup en Python et je suis en train d'extraire dict
de BeautifulSoup.
J'ai utilisé BeautifulSoup pour extraire JSON et a obtenu beautifulsoup.beautifulsoup
variable soup
.
Je vais essayer d'obtenir des valeurs d' soup
, mais quand je fais result = soup.findAll("bill")
- je obtenir une liste vide []
. Comment puis-je extraire de la soupe pour obtenir dict
résultat de:
{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
=> résultat ci-dessous
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"
}
Mise à JOUR
Voici comment je l'ai eu 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)
BeautifulSoup
est utilisé pour travailler avec html/xml. D'où vient json viennent les données? Est-il intégrer en html? Si oui, veuillez fournir le code html correspondant fragment.- Pourquoi êtes-vous à l'aide de BeautifulSoup? On dirait que tous vous avez besoin est
json_decode
...
Vous devez vous connecter pour publier un commentaire.
Pas très familier avec BeautifulSoup, mais si vous avez juste besoin de décodage JSON
ValueError: No JSON object could be decoded
No JSON object
parce que Beautifulsoup était de retourner un ensemble de balise de script, donc j'ai juste besoin de fairenewDictionary=json.loads(str(soup.text))
Vous pouvez supprimer
BeautifulSoup
: