从bs4.element.Tag获取包含JSON的文本(beautifulsoup)



我有一个bs4.element.Tag类型的元素,比如:

<span class="hidden gtm-product-data">
{"id":"716727616125500","name":"chinese whitewine","price":7.99,"brand":"silver heights"}
</span>

span标记包含一个JSON字符串。如何只提取品牌名称(JSON字段brand的值(silver heights

这就是您需要的吗?

import json
from bs4 import BeautifulSoup
sample = '<span class="hidden gtm-product-data">{"id":"716727616125500","name":"chinese white wine","price":7.99,"brand":"silver heights"}</span>'
soup = json.loads(BeautifulSoup(sample, 'html.parser').find("span").getText())["brand"]
print(soup)

输出:

silver heights

最新更新