【问题标题】:Parsing Javascript text in Python在 Python 中解析 Javascript 文本
【发布时间】:2017-11-04 06:46:50
【问题描述】:

我正在解析这段 HTML

<script type="text/javascript">
    var spConfig = new Product.Config({"attributes":{"150":{"id":"150","code":"size_shoe","label":"Schuhgr\u00f6\u00dfe","options":[{"id":"494","label":"36","price":"0","oldPrice":"0","products":["393318"],"label_us":"\r\n4Y","label_uk":"3.5","label_cm":"23","label_int":null},{"id":"476","label":"36.5","price":"0","oldPrice":"0","products":["393321"],"label_us":"\r\n4.5Y","label_uk":"4","label_cm":"23.5","label_int":null,"out_of_stock":"Out of Stock"},{"id":"130","label":"37.5","price":"0","oldPrice":"0","products":["393324"],"label_us":"\r\n5Y","label_uk":"4.5","label_cm":"23.5","label_int":null,"out_of_stock":"Out of Stock"},{"id":"12","label":"38","price":"0","oldPrice":"0","products":["393327"],"label_us":"\r\n5.5Y","label_uk":"5","label_cm":"24","label_int":null},{"id":"500","label":"38.5","price":"0","oldPrice":"0","products":["393330"],"label_us":"\r\n6Y","label_uk":"5.5","label_cm":"24","label_int":null,"out_of_stock":"Out of Stock"},{"id":"10","label":"40","price":"0","oldPrice":"0","products":["393333"],"label_us":"\r\n7Y","label_uk":"6","label_cm":"25","label_int":null,"out_of_stock":"Out of Stock"}]}},"template":"\u20ac#{price}","basePrice":"89.95","oldPrice":"89.95","productId":"393306","chooseText":"Choose an Option...","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":19,"currentTax":19,"inclTaxTitle":"Incl. Tax"}});
</script>

这是我的代码

import urllib2, requests, json, re
from bs4 import BeautifulSoup as bs

url = ("link")

session = requests.session()
response = session.get(url)
soup = bs(response.text, 'html.parser')
scripts = soup.findAll('script')


def getIds():
    for script in scripts:
        if 'spConfig =' in script.getText():
            regex = re.compile(r'var spConfig = new Product.Config\((.*?)\);')
            match = regex.search(script.getText())
            spConfig = json.loads(match.groups()[0])
            for key in spConfig['attributes']: # Should only call once
                for product in spConfig['attributes'][key]['options']:
                    if product['label_us']:
                        size_id = product['id']
                        product_id = spConfig['attributes'][key]['id']
                        print product_id
getIds()

我的输出是

150
150
150
150
150
150
150
150
150
150
150
150
150
150
150
150

我要解析并得到脚本输出的是;

393318 - 4.Y
393321 - 4.5Y - Out of Stock
393324 - 5Y - Out of Stock

等等等等

我错误地解析了哪些属性?另外,如我的示例所示,如何使用文本大小格式化输出并在需要时包括“缺货”?

【问题讨论】:

标签: javascript python html parsing beautifulsoup


【解决方案1】:

您正在打印id 属性,但您要打印的是来自options 对象的productslabel_us。由于product 是一个列表,因此您需要遍历它。

for key, attribute in spConfig['attributes'].iteritems(): 
    for option in attribute['options']:
        if option['label_us']:
            label = option['label_us'].strip()
            for product_id in option['products']:
                print product_id, "-", label

【讨论】:

  • 非常感谢您直截了当的回答。它解决了我在项目的那个部分的问题!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-02
  • 2019-07-05
  • 1970-01-01
  • 2017-07-15
  • 1970-01-01
  • 2011-04-08
  • 2020-01-17
相关资源
最近更新 更多