Python Forum
'NavigableString' object has no attribute 'h2'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'NavigableString' object has no attribute 'h2'
#1
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'
Reply
#2
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
Reply
#3
(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
Reply
#4
I expected you to still get an error, I wanted to see the results of the print statement
Reply
#5
(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
Reply
#6
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to fix "'dict_values' object has no attribute 'inject_wsgi'" in session_transacti devid 0 1,138 Aug-13-2023, 07:52 AM
Last Post: devid
  AttributeError: 'ellipsis' object has no attribute 'register_blueprint' Mechanicalpixelz 2 2,354 Dec-29-2021, 01:30 AM
Last Post: Mechanicalpixelz
  AttributeError: ResultSet object has no attribute 'get_text' KatMac 1 4,336 May-07-2021, 05:32 PM
Last Post: snippsat
  Python 3.9 : BeautifulSoup: 'NoneType' object has no attribute 'text' fudgemasterultra 1 8,812 Mar-03-2021, 09:40 AM
Last Post: Larz60+
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 4 10,381 Nov-11-2019, 05:15 PM
Last Post: nazmulfinance
  AttributeError: 'str' object has no attribute 'xpath' nazmulfinance 0 3,018 Nov-10-2019, 09:13 PM
Last Post: nazmulfinance
  form.populate_obj problem "object has no attribute translate" pascale 0 3,619 Jun-12-2019, 07:30 PM
Last Post: pascale
  Error object has no attribute text hcyeap 3 13,837 May-21-2019, 07:12 AM
Last Post: buran
  AttributeError: 'Response' object has no attribute 'replace' Truman 12 23,164 Mar-20-2019, 12:59 AM
Last Post: ichabod801
  AttributeError: 'dict' object has no attribute 'is_active' (PyMongo And Flask) usman 0 4,918 Nov-20-2018, 09:50 PM
Last Post: usman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020