Reputation: 37
I tried to parse json data but it didn't work, json parser return a strong not a dictionary!! Here the code:
import urllib2
from BeautifulSoup import BeautifulSoup
import json
html = urllib2.urlopen("http://www.imdb.com//name/nm0425005/mediaviewer/rm244453632?ref_=nmmi_mi_all_sf_49").read()
soup = BeautifulSoup(html)
script = soup.find('script', {'id': 'imageJson'})
json_data = ''.join(map(str, script.contents))
json_data = json.dumps(json_data.strip(' \t\n\r'))
data = json.loads(json_data)
print data['mediaViewerModel']
Upvotes: 0
Views: 1425
Reputation: 14847
json_data = ''.join(map(str, script.contents))
>>> json_data = json.dumps(json_data.strip(' \t\n\r'))
data = json.loads(json_data)
The marked line is the problem. json_data
is currently a string encoding a dictionary, and when you call json.dumps
, it will then be a string encoding a string encoding a dictionary, and your last line just undoes one instance. What is it that you're actually trying to do with the marked line?
Upvotes: 1