python - 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?
解决方案
<div>
在尝试查找之前检查是否有汤td
。如果<div>
您指定的类没有,那么div
对象将为无(我们可以轻松检查)。
将代码中的最后一行替换为:
div = soup.find('div', {'class': 'bar-chart-content'})
if div is not None:
table = div..find_all('td')
# Process futher
推荐阅读
- angular - Angular - HTTP拦截器重试具有特定错误状态的请求?
- python - 在列表中循环并将项目分配给新列表 - 有线行为
- mysql - mysql 多行 INSERT 语法,其中一个占位符实际上是一个常量
- java - CursorIndexOutOfBoundsException:请求索引 21,大小为 21
- python-3.x - 如何合并同一张图上的两个图?
- android - 如果应用程序在离线模式下启动,连接到互联网后进度对话框应该隐藏
- java - 如何在 Spring WebClient 中设置和处理超时?
- python - 在 python 中使用 astropy 裁剪 .fits 图像中的对象
- c# - 如何在 C# 中高效下载、读取和处理 CSV
- microsoft-teams - 是否可以选择创建类似 Snapchat 的功能插件以与视频通话进行交互?