I am trying to scrape some contents on a website. I keep getting this error, 'NoneType' object has no attribute 'text' but I don't know how to fix this.
I noticed that the error has to do with this line tr.find("a", class_="sc-dakcWe sc-liNYZW cPIBpC").text.replace("\n", " ") but I have been stucked on how to fix it. After removing the .text.replace("\n", " ") part from it, I get the response as None. I realise my issue is how to get the correct selector for it, what could I change this tr.find("a", class_="sc-dakcWe sc-liNYZW cPIBpC") to that would give me the correct restaurant_name.
I am using the zomato restaurant and an example url for this is https://www.zomato.com/kanpur/top-restaurants
python code
import requests
from bs4 import BeautifulSoup
city = input("Enter your city: ")
url = "https://www.zomato.com/" + city + "/top-restaurants"
header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36"}
response = requests.get(url, headers=header)
html = response.text
soup = BeautifulSoup(html, "html.parser")
print(soup.title.text)
top_rest = soup.find("div", class_="bke1zw-0 cMipmx")
list_tr = top_rest.find_all("div", class_="bke1zw-1")
for tr in list_tr:
restaurant_name = tr.find("a", class_="sc-dakcWe sc-liNYZW cPIBpC").text.replace("\n", " ")
print(restaurant_name)