gpt4 book ai didi

python - Beautifulsoup 不返回 href 它返回 None

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

>>> soup_brand
<a data-role="BRAND" href="/URL/somename">
Some Name
</a>

>>> type(soup_brand)
<class 'bs4.BeautifulSoup'>

>>> print(soup_brand.get('href'))
None
文档如下: https://www.crummy.com/software/BeautifulSoup/bs4/doc/
嗨,来自世界各地的人们,
有人现在出了什么问题还是我的目标对象错了?
需要获取href。

最佳答案

你有没有尝试过:

soup.find_all(name="a")
或者
soup.select_one(selector="a")
也应该可以 catch
all_anchor_tags = soup.find_all(name="a")
for tag in all_anchor_tags:
print(tag.get("href")) #prints the href element of each a tag, thus each link
虽然我遇到的所有 bs4 寻找多个元素(我们在这里有一个循环的原因),但 bs4 有时在捕捉事物方面更好,如果你给它一个搜索所有方法然后迭代元素

关于python - Beautifulsoup 不返回 href 它返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66422987/

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