简体   繁体   English

如何在Python中使用Javascript对象文字

[英]How do I work with Javascript Object Literals in Python

Through Beautifulsoup module I have extracted an HTML page. 通过Beautifulsoup模块,我提取了一个HTML页面。 From that page, I further extracted a Javascript script tag. 从该页面,我进一步提取了Javascript脚本标签。 Inside the script tag, there is an object literal that I would like to work with. 在script标记内,有一个我想使用的对象文字。 You can see what I would like to achieve: 您可以看到我想要实现的目标:

<script>
            var main_obj = {

            "link":"",
            "stock":"",
            "price":[{"qty":1000,"value":"100$"}, {"qty":10000,"value":"1000$"}]

           } 

</script>

I would like to access the qty and value variables inside the object literals of price variable which is inside main_obj. 我想访问位于main_obj内部的price变量的对象常量内的qty和value变量。 Thankyou 谢谢

One option would be to use selenium . 一种选择是使用selenium In particular, you can use execute_script to convert to a JSON string which Python can easily parse. 特别是,您可以使用execute_script转换为Python可以轻松解析的JSON字符串。

Since I don't know what the URL of the page you are working with is, I just created a local HTML file which included your script tag example. 由于我不知道您正在使用的页面的URL是什么,因此我刚刚创建了一个本地HTML文件,其中包含您的脚本标签示例。 Using headless is not mandatory and I only added that option so the browser window doesn't open. 使用无头不是必须的,我只是添加了该选项,因此浏览器窗口不会打开。

test.html

<!DOCTYPE html>
<html>
<body>
<script>
    var main_obj = {
        "link": "",
        "stock": "",
        "price": [{"qty": 1000, "value": "100$"}, {"qty": 10000, "value": "1000$"}]
    }
</script>
</body>
</html>

Script 脚本

In[2]: import os
  ...: import json
  ...: 
  ...: from selenium import webdriver
  ...: 
  ...: chrome_options = webdriver.ChromeOptions()
  ...: chrome_options.add_argument('--headless')
  ...: driver = webdriver.Chrome(chrome_options=chrome_options)
  ...: 
  ...: driver.get('file://{}/test.html'.format(os.getcwd()))
  ...: json_string = driver.execute_script('return JSON.stringify(main_obj)')
  ...: driver.quit()
  ...: 
  ...: json_data = json.loads(json_string)
In[3]: json_data
Out[3]: 
{'link': '',
 'stock': '',
 'price': [{'qty': 1000, 'value': '100$'}, {'qty': 10000, 'value': '1000$'}]}
In[4]: for item in json_data['price']:
  ...:     print('Quantity: {:d}\tValue: ${:.2f}'.format(
  ...:         item['qty'], float(item['value'].rstrip('$'))
  ...:     ))
  ...: 
Quantity: 1000  Value: $100.00
Quantity: 10000 Value: $1000.00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM