BeautifulSoup:AttributeError:“NavigableString”对象没有属性“name”

2024-04-22

你知道为什么 BeautifulSoup 教程中的第一个例子吗http://www.crummy.com/software/BeautifulSoup/documentation.html#QuickStart http://www.crummy.com/software/BeautifulSoup/documentation.html#QuickStart gives AttributeError: 'NavigableString' object has no attribute 'name'?根据这个答案 http://groups.google.com/group/beautifulsoup/browse_thread/thread/5359ec13be2347d5/2f8fad5e788fe9e0HTML 中的空格字符会导致问题。我尝试了几页的源代码,其中 1 个有效,其他的也给出了相同的错误(我删除了空格)。您能解释一下“名称”指的是什么以及为什么会发生此错误吗?谢谢。


直接无视(好了NavigableString遍历树时的对象:

from bs4 import BeautifulSoup, NavigableString, Tag

response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

for body_child in soup.body.children:
    if isinstance(body_child, NavigableString):
        continue
    if isinstance(body_child, Tag):
        print(body_child.name)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

BeautifulSoup:AttributeError:“NavigableString”对象没有属性“name” 的相关文章

随机推荐