簡體   English   中英

錯誤:“。NoneType”對象沒有.find的屬性“ find_all”

[英]Error: 'NoneType' object has no attribute 'find_all' for .find

我不熟悉網絡竊聽,並試圖從Google趨勢網站獲取有關所用關鍵字平均值的信息。

當前,我收到錯誤$ AttributeError:'NoneType'對象沒有屬性'find_all'。 我認為該錯誤是因為'bar-chart-content'作為一個類可能不存在為HTML中的名稱。

# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup

quote_page = 'https://trends.google.com/trends/explore?geo=US&q=pikachu,Cat'

page = requests.get(quote_page)

soup = BeautifulSoup(page.text, 'html.parser')

table = soup.find('div', {'class': 'bar-chart-content'}).find_all('td') #Gives the error: AttributeError: 'NoneType' object has no attribute 'find_all'

請告訴我如何解決此問題,以及有關將來如何為不包括inspect的網站找到正確的類名稱的任何建議[如果是問題]?

編輯:基於MePsyDuck的答案,該類不存在,因此如何找到正確的名稱?

在嘗試找到td之前,只需要檢查一下湯是否找到了<div>即可。 如果您指定的類中沒有<div> ,則div對象將為None(我們可以輕松檢查)。

將代碼中的最后一行替換為:

div = soup.find('div', {'class': 'bar-chart-content'})
if div is not None:
    table = div..find_all('td') 
    # Process futher

暫無
暫無

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

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