Python Forum
'NavigableString' object has no attribute 'h2' - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: 'NavigableString' object has no attribute 'h2' (/thread-26924.html)



'NavigableString' object has no attribute 'h2' - RandomCoder - May-19-2020

Good Afternoon!
I have been following a tutorial on beautiful soup by Corey Schafer, but I came across an error when writing the code, and I do not know how I can debug it, considering the code is the same as it is in the video. Could this be due to how old/outdated the information on the video could be? and is there a way to debug this program? Thank you!
from bs4 import BeautifulSoup
import requests

with open('simple.html') as html_file:
    soup = BeautifulSoup(html_file, 'lxml')

for article in soup.find('div', class_='footer'):
    headline = (article.h2.a.text)
    print(headline)

    summary = (article.p.text)
    print(summary)

    print()
Error:
Traceback (most recent call last): File "/Users/name/Desktop/untitled folder 2/covidscraper.py", line 8, in <module> headline = (article.h2.a.text) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/bs4/element.py", line 885, in __getattr__ self.__class__.__name__, attr)) AttributeError: 'NavigableString' object has no attribute 'h2'



RE: 'NavigableString' object has no attribute 'h2' - Larz60+ - May-19-2020

The error is telling you that there is something wrong with navigation from article-->h2
wither there is another node or nodes between 'div' tag and h2 tag, or h2 doesn't exist
add print(article) immediately after line 7 print(article) and post results


RE: 'NavigableString' object has no attribute 'h2' - RandomCoder - May-19-2020

(May-19-2020, 01:41 AM)Larz60+ Wrote: The error is telling you that there is something wrong with navigation from article-->h2
wither there is another node or nodes between 'div' tag and h2 tag, or h2 doesn't exist
add print(article) immediately after line 7 print(article) and post results
I still get the same error with no changes


RE: 'NavigableString' object has no attribute 'h2' - Larz60+ - May-19-2020

I expected you to still get an error, I wanted to see the results of the print statement


RE: 'NavigableString' object has no attribute 'h2' - RandomCoder - May-19-2020

(May-19-2020, 07:07 PM)Larz60+ Wrote: I expected you to still get an error, I wanted to see the results of the print statement
But there isn't anything else that the program prints out to begin with, it still returns just the same error, even with the changes made there is nothing else being printed out other than the same error


RE: 'NavigableString' object has no attribute 'h2' - Larz60+ - May-20-2020

It's possible that article is empty, change the print to
print(f"Contents of article: {article}")
At least you should see the text from message (just before the error)