BeautifulSoup: AttributeError: ‘NavigableString’ object has no attribute ‘name’

Just ignore NavigableString objects while iterating through the tree:

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)

Leave a Comment