Python

AttributeError: 'NoneType' object has no attribute 'get_text' - Python

AttributeError: 'NoneType' object has no attribute 'get_text' - Python, someone asked me to explain?

This program to extract the text from an HTML address tag using Python.

CODE:

import requests
from bs4 import BeautifulSoup

url = 'https://www.justdial.com/Pune/Amrit-Guest-House-Behind-Hoshtel99-Koregaon-Park/020PXX20-XX20-240110203615-G5H8_BZDET?ncatid=10255012&area=&search=Top%20Amrit%20Guest%20House%20Behind%20Hoshtel99%20Koregaon%20Park%20in%20Pune&mncatname=Amrit%20Guest%20House%20Behind%20Hoshtel99%20Koregaon%20Park&search_id=a0b376a253daae9a9afb91e2da4229f66d238683c1e07ff9daf9f023b4fef7e8&abd_btn=&abd_heading=&isFreetxt=1&bd=2'  # Replace with the actual URL

response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
address_tag = soup.find('address')
address_text = address_tag.get_text(strip=True)
print("Text from <address> tag:")

print(address_text)

I got this following error while running the above code.

AttributeError NoneType

Solution:

import requests
from bs4 import BeautifulSoup

url = 'https://www.justdial.com/Pune/Amrit-Guest-House-Behind-Hoshtel99-Koregaon-Park/020PXX20-XX20-240110203615-G5H8_BZDET?ncatid=10255012&area=&search=Top%20Amrit%20Guest%20House%20Behind%20Hoshtel99%20Koregaon%20Park%20in%20Pune&mncatname=Amrit%20Guest%20House%20Behind%20Hoshtel99%20Koregaon%20Park&search_id=a0b376a253daae9a9afb91e2da4229f66d238683c1e07ff9daf9f023b4fef7e8&abd_btn=&abd_heading=&isFreetxt=1&bd=2'  # Replace with the actual URL

headers = { "User-Agent" : "My Agent" }
response = requests.get(url, headers= headers)
soup = BeautifulSoup(response.content, 'html.parser')
address_tag = soup.find('address')
address_text = address_tag.get_text(strip=True)
print("Text from <address> tag:")
print(address_text)

VIDEO GUIDE:

Post your comments / questions