在json python中查找所有标签和产品

在json python中查找所有标签和产品,json,python-3.x,web-scraping,beautifulsoup,Json,Python 3.x,Web Scraping,Beautifulsoup,我试图在text/javascript中找到所有标签和产品,但我不知道如何做到这一点。我正在尝试解析标签、id和产品 var spConfigDisabledProducts = [-1 , '290058', '290060', '290061', '290062', '290063', '290065', '290071', '290073', '290075', '290076', '290077', '290078' ]; var spConfig

我试图在text/javascript中找到所有标签和产品,但我不知道如何做到这一点。我正在尝试解析标签、id和产品

    var spConfigDisabledProducts = [-1
    , '290058', '290060', '290061', '290062', '290063', '290065', '290071', '290073', '290075', '290076', '290077', '290078'        ];
    var spConfig = new Product.Config({"attributes":{"959":{"id":"959","code":"aw_taglia","label":"Taglia","options":[{"id":"730","label":"36 ½","price":"0","oldPrice":"0","products":["290058"]},{"id":"731","label":"37 ½","price":"0","oldPrice":"0","products":["290060"]},{"id":"732","label":"38","price":"0","oldPrice":"0","products":["290061"]},{"id":"733","label":"38 ½","price":"0","oldPrice":"0","products":["290062"]},{"id":"734","label":"39","price":"0","oldPrice":"0","products":["290063"]},{"id":"735","label":"40","price":"0","oldPrice":"0","products":["290064"]},{"id":"736","label":"40 ½","price":"0","oldPrice":"0","products":["290065"]},{"id":"737","label":"41","price":"0","oldPrice":"0","products":["290066"]},{"id":"738","label":"42","price":"0","oldPrice":"0","products":["290067"]},{"id":"739","label":"42 ½","price":"0","oldPrice":"0","products":["290068"]},{"id":"740","label":"43","price":"0","oldPrice":"0","products":["290069"]},{"id":"741","label":"44","price":"0","oldPrice":"0","products":["290070"]},{"id":"742","label":"44 ½","price":"0","oldPrice":"0","products":["290071"]},{"id":"743","label":"45","price":"0","oldPrice":"0","products":["290072"]},{"id":"744","label":"45 ½","price":"0","oldPrice":"0","products":["290073"]},{"id":"745","label":"46","price":"0","oldPrice":"0","products":["290074"]},{"id":"746","label":"47","price":"0","oldPrice":"0","products":["290075"]},{"id":"747","label":"47 ½","price":"0","oldPrice":"0","products":["290076"]},{"id":"748","label":"13.5","price":"0","oldPrice":"0","products":["290077"]},{"id":"749","label":"48 ½","price":"0","oldPrice":"0","products":["290078"]}]}},"template":"#{price}\u00a0\u20ac","basePrice":"130","oldPrice":"130","productId":"290059","chooseText":"Seleziona","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"Incl. Tasse"}});
    jQuery("#attribute959 option").each(function () {
        var option = jQuery(this);
        var id = option.attr('value');
        jQuery.each(spConfig.config.attributes, function () {
            jQuery.each(this.options, function () {
                if (this.id == id) {
                    if (spConfigDisabledProducts.indexOf(this.products[0]) >= 0) {
                        option.data('disabled', true);
                    }
                }
            });
        });
    });

您可以将javascript对象正则化并传递给json,然后解析出信息

import re
import json

#html = response.content from requests
html = '''
var spConfigDisabledProducts = [-1
    , '290058', '290060', '290061', '290062', '290063', '290065', '290071', '290073', '290075', '290076', '290077', '290078'        ];
    var spConfig = new Product.Config({"attributes":{"959":{"id":"959","code":"aw_taglia","label":"Taglia","options":[{"id":"730","label":"36 ½","price":"0","oldPrice":"0","products":["290058"]},{"id":"731","label":"37 ½","price":"0","oldPrice":"0","products":["290060"]},{"id":"732","label":"38","price":"0","oldPrice":"0","products":["290061"]},{"id":"733","label":"38 ½","price":"0","oldPrice":"0","products":["290062"]},{"id":"734","label":"39","price":"0","oldPrice":"0","products":["290063"]},{"id":"735","label":"40","price":"0","oldPrice":"0","products":["290064"]},{"id":"736","label":"40 ½","price":"0","oldPrice":"0","products":["290065"]},{"id":"737","label":"41","price":"0","oldPrice":"0","products":["290066"]},{"id":"738","label":"42","price":"0","oldPrice":"0","products":["290067"]},{"id":"739","label":"42 ½","price":"0","oldPrice":"0","products":["290068"]},{"id":"740","label":"43","price":"0","oldPrice":"0","products":["290069"]},{"id":"741","label":"44","price":"0","oldPrice":"0","products":["290070"]},{"id":"742","label":"44 ½","price":"0","oldPrice":"0","products":["290071"]},{"id":"743","label":"45","price":"0","oldPrice":"0","products":["290072"]},{"id":"744","label":"45 ½","price":"0","oldPrice":"0","products":["290073"]},{"id":"745","label":"46","price":"0","oldPrice":"0","products":["290074"]},{"id":"746","label":"47","price":"0","oldPrice":"0","products":["290075"]},{"id":"747","label":"47 ½","price":"0","oldPrice":"0","products":["290076"]},{"id":"748","label":"13.5","price":"0","oldPrice":"0","products":["290077"]},{"id":"749","label":"48 ½","price":"0","oldPrice":"0","products":["290078"]}]}},"template":"#{price}\u00a0\u20ac","basePrice":"130","oldPrice":"130","productId":"290059","chooseText":"Seleziona","taxConfig":{"includeTax":true,"showIncludeTax":true,"showBothPrices":false,"defaultTax":0,"currentTax":0,"inclTaxTitle":"Incl. Tasse"}});
    jQuery("#attribute959 option").each(function () {
        var option = jQuery(this);
        var id = option.attr('value');
        jQuery.each(spConfig.config.attributes, function () {
            jQuery.each(this.options, function () {
                if (this.id == id) {
                    if (spConfigDisabledProducts.indexOf(this.products[0]) >= 0) {
                        option.data('disabled', true);
                    }
                }
            });
        });
    });'''

p = re.compile(r'Product\.Config\((.*?)\)', re.DOTALL)
data = json.loads(p.findall(html)[0])

for attribute in data['attributes']:
    print('-----------------attribute--------------')
    print('label = ' + data['attributes'][attribute]['label'], 'id = ' + data['attributes'][attribute]['id'])
    print('-----------------options----------------')
    for product in data['attributes'][attribute]['options']:
        print('label = ' + product['label'], 'id = ' + product['id'], 'product = ' + product['products'][0])

这样做可能不那么优雅,但不需要正则表达式-只需一系列拆分和清理:

import json

code = [your script above]

code2 = html.replace('½','').split('":"Taglia","options":[{')[1].split('}]}},"template"')[0].split('},{')

for i in range(len(code2)):
    data = json.loads('{'+code2[i]+'}')
    print(data['id'],data['label'])
输出:

730 36 
731 37 
732 38
733 38 

等等。

您好,在我的代码中,我只需要所有id和标签,而不是“var spConfigDisabledProducts=”中的id和标签,您能帮我吗?非常感谢您,在我的代码中,似乎我只需要所有id和标签,而不需要“var spConfigDisabledProducts=”中的id和标签,您能帮我吗?非常感谢您提供的html中会有哪些值?