gpt4 book ai didi

javascript - 使用 re.findall 解析 javascript

转载 作者:行者123 更新时间:2023-12-02 13:45:55 24 4
gpt4 key购买 nike

所以我有几个问题需要解决。

首先,我尝试解析从 html 获得的 JavaScript。

$(document).ready(function() { $('#commodity-show-thumbnails').bxSlider({ mode: 'vertical', auto: false, controls: true, pager: false, minSlides: 4, maxSlides: 4, moveSlides: 1, slideWidth: 250 }); itemSelector('commodity-show-form', 'commodity-show-addcart-submit', [['color', 'Choose color'], ['size', 'Choose size']], { "39805": { "params": ["Smokey Blue/Mica Blue", "36"]}, "39806": { "params": ["Smokey Blue/Mica Blue", "36,5"]}, "39807": { "params": ["Smokey Blue/Mica Blue", "37,5"]}, "39808": { "params": ["Smokey Blue/Mica Blue", "38"]}, "39809": { "params": ["Smokey Blue/Mica Blue", "38,5"]}, "39810": { "params": ["Smokey Blue/Mica Blue", "39"]}, "39811": { "params": ["Smokey Blue/Mica Blue", "40"]}, "39812": { "params": ["Smokey Blue/Mica Blue", "40,5"]}, "39814": { "params": ["Smokey Blue/Mica Blue", "42"]} }, [39805,39806,39807,39808,39809,39810,39811,39812,39814], 'main-cart', 'commodity-show-image'); });

res = re.findall(r'{ "params": (.+?)}', text)  # text is where javascript text is stored

final = [eval(i) for i in res]

print(final)

我得到以下输出

[['烟熏蓝/云母蓝', '36'], ['烟熏蓝/云母蓝', '36,5'], ['烟熏蓝/云母蓝', '37,5'], ['烟熏蓝/云母蓝', '38'], ['烟熏蓝/云母蓝', '38,5'], ['烟熏蓝/云母蓝', '39'], ['烟熏蓝/云母蓝', '39']蓝色', '40'], ['烟熏蓝/云母蓝', '40,5'], ['烟熏蓝/云母蓝', '42']]

但是现在我不知道如何从这里开始。我想从中找到这个值39805

{ "39805": { "params": ["烟蓝色/云母蓝", "36"]}.我该如何解析它,这样如果我正在寻找与 36 相关的值,它会给我 39805?

我很抱歉,但我对解析真的很糟糕,而且我对此还很陌生。

最佳答案

你可以这样得到36:

import re
import ast

a="""$(document).ready(function() { $('#commodity-show-thumbnails').bxSlider({ mode: 'vertical', auto: false, controls: true, pager: false, minSlides: 4, maxSlides: 4, moveSlides: 1, slideWidth: 250 }); itemSelector('commodity-show-form', 'commodity-show-addcart-submit', [['color', 'Choose color'], ['size', 'Choose size']], { "39805": { "params": ["Smokey Blue/Mica Blue", "36"]}, "39806": { "params": ["Smokey Blue/Mica Blue", "36,5"]}, "39807": { "params": ["Smokey Blue/Mica Blue", "37,5"]}, "39808": { "params": ["Smokey Blue/Mica Blue", "38"]}, "39809": { "params": ["Smokey Blue/Mica Blue", "38,5"]}, "39810": { "params": ["Smokey Blue/Mica Blue", "39"]}, "39811": { "params": ["Smokey Blue/Mica Blue", "40"]}, "39812": { "params": ["Smokey Blue/Mica Blue", "40,5"]}, "39814": { "params": ["Smokey Blue/Mica Blue", "42"]} }, [39805,39806,39807,39808,39809,39810,39811,39812,39814], 'main-cart', 'commodity-show-image'); });"""
b = re.findall(r'.*?({ ".*?} }).*}', a)[0]

d1 = ast.literal_eval(b)
print d1, '\n'

for a,b in d1.iteritems():
if b['params'][1]=='36':
print a

输出:

{'39809': {'params': ['Smokey Blue/Mica Blue', '38,5']}, '39808': {'params': ['Smokey Blue/Mica Blue', '38']}, '39805': {'params': ['Smokey Blue/Mica Blue', '36']}, '39807': {'params': ['Smokey Blue/Mica Blue', '37,5']}, '39806': {'params': ['Smokey Blue/Mica Blue', '36,5']}, '39812': {'params': ['Smokey Blue/Mica Blue', '40,5']}, '39814': {'params': ['Smokey Blue/Mica Blue', '42']}, '39810': {'params': ['Smokey Blue/Mica Blue', '39']}, '39811': {'params': ['Smokey Blue/Mica Blue', '40']}} 

39805

关于javascript - 使用 re.findall 解析 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41400382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com