gpt4 book ai didi

python - 如何使用 BeautifulSoup 查找带有类的 href 链接

转载 作者:行者123 更新时间:2023-12-01 12:10:11 25 4
gpt4 key购买 nike

<div data-pet-card="pet-card" class="pet-card">

<a data-pet-card="pet-card-link" href="https://Link-I-Want.com"
class="pet-card__link">

我习惯于用 BS4 抓取 html,但我对 html 本身并不是很熟悉,也没有遇到过一个也有类和 data-pet-card="pet-card-link" 的 href。事物。我试过:
for a in soup.find_all('a', href=True):
print("Found the URL:", a['href'])

但它什么也不打印,也没有错误。

有什么用,谢谢。

最佳答案

您放入 find_all 中的属性电话是你拥有的东西,而不是你想要找到的东西。在这里你有这个类,所以使用它:

for a in soup.find_all('a', class_="pet-card__link"):
print("Found the URL:", a['href'])

(因为 class是Python中的保留字,这里需要使用 class_。)

关于python - 如何使用 BeautifulSoup 查找带有类的 href 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52332361/

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