簡體   English   中英

我正在嘗試為我的項目抓取一個網站,但我遇到了一個錯誤。 看看,讓我知道是否有人對此有建議

[英]I am trying to scrape a website for my project but I am stuck with an error. Take a look and let me know if anyone have suggestion for this

html_text = requests.get('https://www.trustpilot.com/categories/restaurants_bars? 
numberofreviews=0&status=claimed&timeperiod=0').text
soup = BeautifulSoup(html_text, 'lxml')
restaurants = soup.find_all('a', class_ = 'internal___1jK0Z wrapper___26yB4')
for index, restaurant in enumerate(restaurants):
    name = restaurant.find('div', class_ = 'businessTitle___152-c').text
    rating = restaurant.find('div', class_ = 'textRating___3F1NO').text
    with open('{index}.csv', 'a') as f:
        f.write(name)
        f.write(',')
        f.write(rating)
        f.write('\n')

    print('File saved')

這是引發錯誤的代碼。

回溯(最后一次調用):文件“main.py”,第 8 行,名稱 = restaurant.find('div', class_ = 'businessTitle___152-c').text AttributeError: 'NoneType' object 沒有屬性 'text '

運行您的代碼並打印出相關位。 您所定位的類集, internal___1jK0Z wrapper___26yB4器___26yB4 不僅用於餐廳網站,還用於頁面頁腳中的 about 鏈接。 我認為這組類用於每個鏈接,而不僅僅是餐廳網站的鏈接。

在此處輸入圖像描述

您正在嘗試訪問None類型的 object 的屬性,可能您的一個查詢返回None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM