开发者

Extract javascript variable value from html document with python

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

Something lik开发者_高级运维e 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?


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'}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜