错误:“NoneType”对象没有“find”的“find”属性。find

2024-05-29 06:54:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我是新的网站搜索,并试图从谷歌趋势网站获得有关平均使用关键字的信息。在

当前我收到错误$AttributeError:“NoneType”对象没有“find\u 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的回答,该类不存在,因此如何找到正确的名称?在


Tags: import网站错误chartpagebarcontentall
1条回答
网友
1楼 · 发布于 2024-05-29 06:54:52

在尝试查找td之前,只需检查soup是否找到<div>。 如果指定的类没有<div>,那么{}对象将是None(我们可以很容易地检查)。在

将代码中的最后一行替换为:

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

相关问题 更多 >

    热门问题