1 回答

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊
我想建議一種不同的方法。該屬性值看起來(lái)像JSON,那么為什么不使用json模塊呢?這樣,您就有了一個(gè)現(xiàn)成的數(shù)據(jù)結(jié)構(gòu),可以進(jìn)一步修改。
import json
from bs4 import BeautifulSoup
html_list = [
"""<div class="impressions" data-impressions=\'{"id":"01920","name":"Sleepy","price":12.95,"brand":"Lush","category":"Bubble Bar","variant":"7 oz.","quantity":1,"list":"/bath/bubble-bars/sleepy/9999901920.html","dimension11":"","dimension12":"Naked,Self Preserving,Vegan","dimension13":1,"dimension14":1,"dimension15":true}\'></div>""",
]
data_structures = []
for html_item in html_list:
soup = BeautifulSoup(html_item, "html.parser").find("div", {"class": "impressions"})
data_structures.append(json.loads(soup["data-impressions"]))
print(data_structures)
這會(huì)輸出一個(gè)字典列表:
[{'id': '01920', 'name': 'Sleepy', 'price': 12.95, 'brand': 'Lush', 'category': 'Bubble Bar', 'variant': '7 oz.', 'quantity': 1, 'list': '/bath/bubble-bars/sleepy/9999901920.html', 'dimension11': '', 'dimension12': 'Naked,Self Preserving,Vegan', 'dimension13': 1, 'dimension14': 1, 'dimension15': True}]
要訪(fǎng)問(wèn)所需的密鑰,只需執(zhí)行以下操作:
for data_item in data_structures:
print(data_item["dimension12"])
印刷:Naked,Self Preserving,Vegan
添加回答
舉報(bào)