gpt4 book ai didi

python - 从脚本中提取 - BeautifulSoup

转载 作者:太空宇宙 更新时间:2023-11-04 09:32:25 27 4
gpt4 key购买 nike

“tier1Category”的值如何从此页面的源中提取? https://www.walgreens.com/store/c/walgreens-wal-zyr-24-hour-allergy-tablets/ID=prod6205762-product

soup.find('script') 

仅返回源的一个子集,以下返回该代码中的另一个源。

json.loads(soup.find("script", type="application/ld+json").text)

最佳答案

Bitto 和我对此有类似的方法,但我宁愿不依赖于知道哪个脚本包含匹配模式,也不依赖于脚本的结构。

import requests
from collections import abc
from bs4 import BeautifulSoup as bs

def nested_dict_iter(nested):
for key, value in nested.items():
if isinstance(value, abc.Mapping):
yield from nested_dict_iter(value)
else:
yield key, value

r = requests.get('https://www.walgreens.com/store/c/walgreens-wal-zyr-24-hour allergy-tablets/ID=prod6205762-product')
soup = bs(r.content, 'lxml')
for script in soup.find_all('script'):
if 'tier1Category' in script.text:
j = json.loads(script.text[str(script.text).index('{'):str(script.text).rindex(';')])
for k,v in list(nested_dict_iter(j)):
if k == 'tier1Category':
print(v)

关于python - 从脚本中提取 - BeautifulSoup ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55198824/

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