0

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']
1
  • yes I printed the json data before pass it to json module, yet it looks alright Commented Feb 17, 2017 at 19:48

1 Answer 1

1
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?

Sign up to request clarification or add additional context in comments.

2 Comments

I tried only loads to load the function but it didn't works, I got this error: ValueError: Expecting , delimiter: line 2 column 515 (char 515) I add dumps I thought it will fix the syntax of json
That's telling you that what you're trying to parse isn't valid JSON. Since you're scraping it from a site, you're probably getting something back without proper escaping or something vs. what you'd get from an API.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.