c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
Python

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

| | python

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: