Do you know why the first example in BeautifulSoup tutorial http://www.crummy.com/software/BeautifulSoup/documentation.html#QuickStart gives AttributeError: 'NavigableString' object has no attribute 'name'? According to this answer the space characters in the HTML causes the problem. I tried with sources of a few pages and 1 worked the others gave the same error (I removed spaces). Can you explain what does “name” refer to and why this error happens? Thanks.
Do you know why the first example in BeautifulSoup tutorial http://www.crummy.com/software/BeautifulSoup/documentation.html#QuickStart gives AttributeError: ‘NavigableString’
Share
namewill refer to the name of the tag if the object is aTagobject (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 thecontentsto grab nodes, you might grab aNavigableStringinstead of the nextTag.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
Tagwith that name orNoneif no children with that name exist: Using Tag Names as MembersIf you wanna use the
contentsyou 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 aTag