I have the JSON object:
{
"review_body": "Beef noodles realism weathered modem tanto hotdog dolphin long-chain hydrocarbons 8-bit euro-pop tank-traps Tokyo narrative.-space j-pop franchise otaku faded RAF girl artisanal hotdog denim ablative systemic smart-Kowloon. Man construct dome smart-computer pen monofilament beef noodles rain garage geodesic bicycle San Francisco wonton soup dissident nodal point tower. Boat uplink film dead man modem warehouse. Nodal point jeans euro-pop render-farm nano-fetishism semiotics hacker gang. Futurity narrative youtube otaku Kowloon free-market drugs. Fluidity assassin Tokyo bicycle media assault concrete industrial grade ablative lights boat BASE jump A.I. post-stimulate carbon. Physical computer narrative city youtube math-neural assassin modem.",
"link": "http://www.getlost.com/store/acme/review/10607787#comment10607787",
"seller_id": "104523",
"survey_id": "9933447",
"loggedin_user": 0,
"store_rating": "8.02",
"store_thumb": "http://www.getlost.com/store/thumbnail/acme.jpg",
"store_name": "acme",
"username": "ronin666",
"rating": "1",
"ref": "RR,acme,104523"
}
embedded within
<script LANGUAGE="javascript">
window.commentShare = $.extend((window.commentShare || {}), {
10607787: {
"review_body": "Beef noodles realism weathered modem tanto hotdog dolphin long-chain hydrocarbons 8-bit euro-pop tank-traps Tokyo narrative.-space j-pop franchise otaku faded RAF girl artisanal hotdog denim ablative systemic smart-Kowloon. Man construct dome smart-computer pen monofilament beef noodles rain garage geodesic bicycle San Francisco wonton soup dissident nodal point tower. Boat uplink film dead man modem warehouse. Nodal point jeans euro-pop render-farm nano-fetishism semiotics hacker gang. Futurity narrative youtube otaku Kowloon free-market drugs. Fluidity assassin Tokyo bicycle media assault concrete industrial grade ablative lights boat BASE jump A.I. post-stimulate carbon. Physical computer narrative city youtube math-neural assassin modem.",
"link": "http:\/\/www.getlost.com\/store\/acme\/review\/10607787#comment10607787",
"seller_id": "104523",
"survey_id": "9933447",
"loggedin_user": 0,
"store_rating": "8.02",
"store_thumb": "http:\/\/www.getlost.com\/store\/thumbnail\/acme.jpg",
"store_name": "acme",
"username": "ronin666",
"rating": "1",
"ref": "RR,acme,104523"
}
});
</script>
I'd like to extract the aforementioned JSON object. How can this be achieved? Should I use regular expressions?
How this type of object can be obtained (via Ipython, python 2.7):
I was basically scraping the review site resellerratings.com for an arbitrary store using BeautifulSoup. I obtained the soup object and noticed that there are useful JSON objects containing information for each review at the chosen store. However, upon invoking soup.find("script", language = "javascript"), I'm still left with the JSON object embedded within the script tags.
from mechanize import Browser
import bs4
from bs4 import BeautifulSoup
br = Browser()
br.set_handle_robots(False)
br.set_handle_refresh(False)
example_url = 'http://www.resellerratings.com/store/My_Digital_Palace'
response = br.open(example_url)
soup = BeautifulSoup(response)
soup.find("script", language = "javascript")
This should return:
<script language="javascript">
window.commentShare = $.extend(
(window.commentShare || {}), {
375015: {
"review_body": "I bought a Kodak LS443 form My Digital Palace in 2004. I also purchased a 5 year warranty. Now the camera does not work and I am unable to contact them. What do I do??? Am I just screwed???<br><br>Margaret Fuller<br>[email protected]",
"link": "http:\/\/www.resellerratings.com\/store\/My_Digital_Palace\/review\/375015#comment375015",
"seller_id": "6930",
"survey_id": "385176",
"loggedin_user": 0,
"store_rating": "1.00",
"store_thumb": "http:\/\/www.resellerratings.com\/store\/thumbnail\/My_Digital_Palace.jpg",
"store_name": "My Digital Palace",
"username": "maf1059",
"rating": "1",
"ref": "RR,My_Digital_Pala,6930"
}
}
);
</script>