首页 > 解决方案 > 内存错误使用漂亮的汤用python解析~100mb html文件

问题描述

运行以下代码时出现内存错误:

from bs4 import BeautifulSoup
with open(r'C:\user\table.html', 'r') as f:
    soup = BeautifulSoup(f, 'html.parser')
    found = soup.find("td", text = "A")
    print(found)
f.closed

此代码适用于小型 html 文件。但是,当我使用 ~100mb html 表运行它时,它会慢慢消耗内存,直到程序崩溃。任务管理器显示 python.exe 在程序崩溃时占用 2gb 内存。我试过浏览 beautifulsoup 文档,但我无法弄清楚为什么这个程序占用的内存比我的 html 文件的大小多 20 倍。

标签: pythonbeautifulsoup

解决方案


推荐阅读