Python使用bs4搜索特定的“var”

2024-06-07 23:49:46 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我一直在尝试使用scrape学习一些东西,在这里我成功地搜索到一个站点,它返回大量不同的var值,例如:

var FancyboxI18nClose = 'Close';
var FancyboxI18nNext = 'Next';
var FancyboxI18nPrev = 'Previous';
var PS_CATALOG_MODE = false;
var added_to_wishlist = '.';
var ajax_allowed = true;
var ajaxsearch = true;
var attribute_anchor_separator = '-';
var attributesCombinations = [{"id_attribute":"100","id_attribute_group":"1","attribute":"38_5"},{"id_attribute":"101","id_attribute_group":"1","attribute":"39"},{"id_attribute":"103","id_attribute_group":"1","attribute":"40"},{"id_attribute":"104","id_attribute_group":"1","attribute":"40_5"},{"id_attribute":"105","id_attribute_group":"1","attribute":"41"},{"id_attribute":"107","id_attribute_group":"1","attribute":"42"},{"id_attribute":"108","id_attribute_group":"1","attribute":"42_5"},{"id_attribute":"109","id_attribute_group":"1","attribute":"43"},{"id_attribute":"111","id_attribute_group":"1","attribute":"44"},{"id_attribute":"112","id_attribute_group":"1","attribute":"44_5"},{"id_attribute":"132","id_attribute_group":"1","attribute":"45"},{"id_attribute":"113","id_attribute_group":"1","attribute":"46"}];

当然还有很多,它们都只包含在var中。但是我想做的是只能够刮取其中一个值-var attributesCombinations这意味着我基本上只想打印出这个值,然后我就可以使用它了json.loads文件在那里我也可以更轻松地获取json。你知道吗

我想做的是:

try:
    product_li_tags = bs4.find_all(text=re.compile('attributesCombinations'))
except Exception:
    product_li_tags = []

但这给出了所有“var”开始于attributesCombinations的结果。你知道吗

['var CUSTOMIZE_TEXTFIELD = 1;\nvar FancyboxI18nClose = \'Close\';\nvar FancyboxI18nNext = \'Next\';\nvar FancyboxI18nPrev = \'Previous\';\nvar PS_CATALOG_MODE = false;\nvar added_to_wishlist = \'The product was successfully added to your wishlist.\';\nvar ajax_allowed = true;\nvar ajaxsearch = true;\nvar allowBuyWhenOutOfStock = false;\nvar attribute_anchor_separator = \'-\';\nvar attributesCombinations = [{"id_attribute":"100","id_attribute_group":"1","att...........

如何使它只打印出var attributesCombinations?你知道吗


Tags: toidfalsetrueaddedclosevargroup
2条回答

将从attributesCombinations到语句结尾的部分提取出来的正则表达式是

var attributesCombinations = (\[.*?\])

在Python中,您可以根据需要轻松地创建正则表达式

re.compile(r'var attributesCombinations = (\[.*?\])');

不要在bs4中使用re.compile,直接运行它。你知道吗

match = re.compile('var\s*attributesCombinations\s*=\s*(\[.*?\])').findall(htmlString)
attributesCombinations = json.loads(match[0])
print(attributesCombinations)

相关问题 更多 >

    热门问题