首页 > 解决方案 > Error: 'NoneType' object has no attribute 'find_all' for .find

问题描述

I am new to webscraping and trying to get information from Google Trends website about the Average of keywords used.

Currently I am getting the error $AttributeError: 'NoneType' object has no attribute 'find_all'. I think that the error is because 'bar-chart-content' as a class may not exist as the name in 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'

Please tell me to how to fix this issue and any suggestions on how to find the correct class name in the future for a website excluding inspect [if that is the issue] ?

EDIT: Based on MePsyDuck's answer, the class does not exist so how can find the correct name?

标签: pythonweb-scraping

解决方案


<div>在尝试查找之前检查是否有汤td。如果<div>您指定的类没有,那么div对象将为无(我们可以轻松检查)。

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

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

推荐阅读