错误“ AttributeError:'NoneType'对象没有属性'text'”

时间:2019-09-12 05:50:32

标签: python-3.x web-scraping beautifulsoup python-requests

这是我编写的代码。我无法解决错误。

import requests
from bs4 import BeautifulSoup as bs
import pandas as ps

try:
    city=input("location:").lower() #converts input string into lower case 
    headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36'} #needed for web browser to retrieve, render and facilitate end user interaction with Web content
    req=requests.get("https://www.zomato.com/{}/restaurants".format(city),headers=headers)
    content=req.content #to get the content from website 
    soup=bs(content,"html.parser") #you can use simply bs(req.text,"html.parser")
    toprest=soup.find_all("div",attrs={"class":"col-s-16 search_results mbot"})
    list_rest= toprest[0].find_all("div",attrs={"class":"content"})
    list_restaurant=[]
    for k in list_rest:
        dataframe ={}
        name=(k.find("a",attrs={"class":"result-title hover_feedback zred bold ln24   fontsize0 "}))
        dataframe["RESTAURANT NAME"]=name.text.replace('\n',' ')
        cuisine=(k.find("span",attrs={"class":"col-s-11 col-m-12 nowrap  pl0"}))
        dataframe["CUISINE TYPE"]=cuisine.text.replace('\n',' ')
        location=(k.find("div",attrs={"class":"col-m-16 search-result-address grey-text nowrap ln22"}))
        dataframe["LOCATION"]=location.text.replace('\n',' ')
        cost=(k.find("span",attrs={"class":"col-s-11 col-m-12 pl0"}))
        dataframe["COST FOR TWO"]=cost.text.replace('\n',' ')
        time=(k.find("div",attrs={"class":"col-s-11 col-m-12 pl0 search-grid-right-text "}))
        dataframe["AVAILABLE TIMINGS"]=time.text.replace('\n',' ')
        rating=(k.find("div",attrs={"class":"ta-right floating search_result_rating col-s-4 clearfix"}))
        dataframe["RATING"]=rating.text.replace('\n',' ')
        list_restaurant.append(dataframe)

    df = ps.DataFrame(list_restaurant)
    df.to_csv("{}.csv".format(city),index=False) 
except (requests.exceptions.MissingSchema,IndexError) :
    print("Location not found!!")
except requests.exceptions.ConnectionError:
    print("No Internet connection!!")

输出

我反复得到这个输出

location:hyderabad
Traceback (most recent call last):
  File "zomato-rest.py", line 17, in <module>
    dataframe["RESTAURANT NAME"]=name.text.replace('\n',' ')
AttributeError: 'NoneType' object has no attribute 'text'

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

我尝试了代码,发现您没有正确放置类。以下是对我有用的东西:

for k in list_rest:
    dataframe ={}
    name=(k.find("a",attrs={"class":"result-title hover_feedback zred bold ln24 fontsize0"}))
    dataframe["RESTAURANT NAME"]=name.text.replace('\n','')
    cuisine=(k.find("span",attrs={"class":"col-s-11 col-m-12 nowrap pl0"}))
    dataframe["CUISINE TYPE"]=cuisine.text.replace('\n','')
    location=(k.find("div",attrs={"class":"col-m-16 search-result-address grey-text nowrap ln22"}))
    dataframe["LOCATION"]=location.text.replace('\n','')
    cost=(k.find("span",attrs={"class":"col-s-11 col-m-12 pl0"}))
    dataframe["COST FOR TWO"]=cost.text.replace('\n','')
    time=(k.find("div",attrs={"class":"col-s-11 col-m-12 pl0 search-grid-right-text"}))
    dataframe["AVAILABLE TIMINGS"]=time.text.replace('\n','')
    rating=(k.find("div",attrs={"class":"ta-right floating search_result_rating col-s-4 clearfix"}))
    dataframe["RATING"]=rating.text.replace('\n','')
    list_restaurant.append(dataframe)

让我知道这是否适合您。 :)