BeautifulSoup: AttributeError: 'NavigableString' object has no attribute 'name'
name
will refer to the name of the tag if the object is a Tag
object (ie: <html>
name = "html")
if you have spaces in your markup in between nodes BeautifulSoup will turn those into NavigableString
's. So if you use the index of the contents
to grab nodes, you might grab a NavigableString
instead of the next Tag
.
To avoid this, query for the node you are looking for: Searching the Parse Tree
or if you know the name of the next tag you would like, you can use that name as the property and it will return the first Tag
with that name or None
if no children with that name exist: Using Tag Names as Members
If you wanna use the contents
you have to check the objects you are working with. The error you are getting just means you are trying to access the name property because the code assumes it's a Tag
You can use try catch to eliminate the cases when Navigable String is being parsed in the loop, like this:
for j in soup.find_all(...)
try:
print j.find(...)
except NavigableString:
pass
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)