python - BeautifulSoup 错误:TypeError:“NoneType”类型的对象没有 len()
问题描述
在使用 BeautifulSoup 并解析 url 时,我遇到了这个错误:
Traceback (most recent call last):
File "/Users/justinhudacsko/PycharmProjects/SportsBot/scrape.py", line 8, in <module>
stats_page = BeautifulSoup(comment, "lxml")
File "/usr/local/lib/python3.9/site-packages/bs4/__init__.py", line 310, in __init__
elif len(markup) <= 256 and (
TypeError: object of type 'NoneType' has no len()
我的代码是:
from urllib.request import urlopen
from bs4 import BeautifulSoup, Comment
url = 'https://www.pro-football-reference.com/years/2020/draft.htm'
html = urlopen(url)
soup = BeautifulSoup(html, "lxml")
comment = soup.find(text=lambda text: isinstance(text, Comment) and 'class="table_outer_container"' in text) #THIS RETURNS NONE
stats_page = BeautifulSoup(comment, "lxml")
为什么变量comment
有None
它的值,即使class="table_outer_container"
这个 url 中有实例?
解决方案
find
您使用的方法将仅返回包含的 HTML注释'class="table_outer_container"'
,而我假设您想要获取其类为的元素的内容table_outer_container
您可以按以下方式执行此操作:
from urllib.request import urlopen
from bs4 import BeautifulSoup
url = 'https://www.pro-football-reference.com/years/2020/draft.htm'
html = urlopen(url)
soup = BeautifulSoup(html, "lxml")
table = soup.find('div', class_='table_outer_container')
推荐阅读
- javascript - 数组值编辑使所有值都相同
- operators - XNOR 是否有任何“模”等效表示?
- xml - XSLT 将嵌套的 xml 元素重写为一个带有逗号分隔值字符串的元素
- docker - 关于何时选择虚拟机或物理机而不是容器的指南
- java - 如何从 docker 环境变量中覆盖 java jar application.properties
- javascript - 有没有办法调试如何通过 Chrome 开发者工具设置 HTTP 标头?
- python - ModuleNotFoundError:没有名为“fontawesomefree”的模块
- python - 使用 xml.etree.ElementTree 解析一些元素的问题
- react-native - Expo Android - RN Modal 将导航栏颜色改为白色
- amazon-web-services - 向网络公开 ECS 服务