You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

爬取课程表时出现'NoneType' object has no attribute 'find_all'错误求助

问题解决:AttributeError: 'NoneType' object has no attribute 'find_all'

错误原因

你在查找表格时写错了HTML标签名:代码里用了'tabele',但目标HTML中的表格标签是<table>,导致soup.find()返回None,后续调用table.find_all()触发了AttributeError。

修复后的代码

将标签名修正为'table'即可,完整代码如下:

import requests
from bs4 import BeautifulSoup
import pandas as pd

with open ('o24.html','r') as f:
    content = f.read()
    soup = BeautifulSoup(content, 'lxml')
    # 修正标签名:tabele → table
    table = soup.find('table', class_='tabela')
    header =[]
    rows = []
    for i, row in enumerate(table.find_all('tr')):
        if i == 0:
            header = [el.text.strip() for el in row.find_all('th')]
        else:
            rows.append([el.text.strip() for el in row.find_all('td')])
    print(header)
    for row in rows:
        print(row)

额外建议

为避免类似NoneType错误,建议添加存在性检查,防止查找失败后程序崩溃:

table = soup.find('table', class_='tabela')
if table:
    # 后续处理逻辑
    header =[]
    rows = []
    for i, row in enumerate(table.find_all('tr')):
        if i == 0:
            header = [el.text.strip() for el in row.find_all('th')]
        else:
            rows.append([el.text.strip() for el in row.find_all('td')])
    print(header)
    for row in rows:
        print(row)
else:
    print("未找到目标表格,请检查标签名或class是否正确")

内容的提问来源于stack exchange,提问作者Sok205

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 11:35:38