0

I want to scrape a Javascript line that contains JSON data in Python. For example:

AH4RSearch.listingsJSON = $.parseJSON('{"properties":[{"Price":3695,"PriceFormatted":"3,695","Street":"9251 E Bajada Road"}');

I understand that after I can get the content of $.parseJSON I can use json.loads to store it in JSON format, but how do I get this content from the Javascript line?

1
  • it is only string/text so use standard string functions like split() and slicing [start:end] Commented Nov 14, 2016 at 4:09

2 Answers 2

1

You might need re to extract the data out

import re
import json
your_js_string = """AH4RSearch.listingsJSON = $.parseJSON('{"properties":[{"Price":3695,"PriceFormatted":"3,695","Street":"9251 E Bajada Road"}');"""

m = re.search(r'\$\.parseJSON\(\'(.*?)\'\);', your_js_string)
print json.loads(m.group(1))
# oh, no, your json is broken
Sign up to request clarification or add additional context in comments.

Comments

0

For Python it is only string so use standard string functions - like split() or slicing [start:end].

import json

text = '''AH4RSearch.listingsJSON = $.parseJSON('{"properties":[{"Price":3695,"PriceFormatted":"3,695","Street":"9251 E Bajada Road"}]}')'''

data = json.loads(text[39:-2])

print(data['properties'][0]['Price'])

Comments

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.