开发者

Extract javascript variable value from html document with python

开发者 https://www.devze.com 2023-04-13 03:46 出处:网络
I need to parse an HTML document that contains javascript code with json object. Something lik开发者_高级运维e this:

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

精彩评论

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

关注公众号