>> a.select('a["data-n-6ren">
gpt4 book ai didi

python - 如何使用 Beautiful Soup 按属性值选择标签

转载 作者:太空狗 更新时间:2023-10-30 01:51:09 32 4
gpt4 key购买 nike

我有以下 HTML 片段:

>>> a
<div class="headercolumn">
<h2>
<a class="results" data-name="result-name" href="/xxy> my text</a>
</h2>

仅当属性 data-name="result-name"时,我才尝试选择标题列

我试过:

>>> a.select('a["data-name="result-name""]')

这给出:

ValueError: Unsupported or invalid CSS selector: 

我怎样才能让它工作?

最佳答案

你可以简单地这样做:

soup = BeautifulSoup(html)
results = soup.findAll("a", {"data-name" : "result-name"})

来源:How to find tags with only certain attributes - BeautifulSoup

关于python - 如何使用 Beautiful Soup 按属性值选择标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24984398/

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