【发布时间】:2020-04-13 16:16:05
【问题描述】:
我正在对 Python 进行一些测试,并且一直在学习一些课程,但我现在非常卡住:
urls = []
for h3_tag in soup.find_all("h3"):
a_tag = h3_tag.find('a')
urls.append(a_tag.attrs['href'])
print(urls)
这应该得到一组 h3 中的“a”。确实如此,但是当我添加 .attrs['href'] 或 .text 以获取 URL Anchor 或 URL 本身时,我不断收到此错误: AttributeError: 'NoneType' object has no attribute 'attrs'
我似乎无法解决...
提前致谢
【问题讨论】:
-
所有
h3标签有锚点吗? -
不是全部,没有
-
find如果没有找到该元素,则返回None。 -
好的,所以你需要在尝试使用之前测试
if a_tag is not None。
标签: python beautifulsoup