Shahaf
Shahaf

Reputation: 357

Extract javascript variable value from html document with python

I need to parse an HTML document that contains javascript code with json object.

Something like this:

<html>
   <head>
   </head>
<body>
    <script type="text/javascript">
        myJSONObject = {"name": "steve", "city": "new york"}
    </script>

   <p>Hello World.</p>
</body>
</html>

How can I extract the myJSONObject value with python?

Upvotes: 4

Views: 3342

Answers (1)

phihag
phihag

Reputation: 287855

You can use lxml to parse the HTML, and then extract the JSON:

>>> import lxml.etree,json
>>> s = '''<html><body><script type="text/javascript">
             myJSONObject = {"name": "steve", "city": "new york"}
           </script></body></html>'''
>>> js = lxml.etree.HTML(s).find('.//body/script').text
>>> jsonCode = js.partition('=')[2].strip()
>>> json.loads(jsonCode)
{u'city': u'new york', u'name': u'steve'}

Upvotes: 8

Related Questions