Conda环境已装html5lib,pandas read_html仍报错及非标准表格读取
问题描述
使用pandas的read_html函数读取由<div>标签构建的网页表格时,触发ImportError错误,提示“html5lib not found, please install it”。但已在Conda环境中安装html5lib,且无法正常导入该库。
错误信息
Se produjo una excepción: ImportError html5lib not found, please install it File "C:\Inversiones\Test2.py", line 24, in <module> df = pd.read_html(html) ^^^^^^^^^^^^^^^^^^ ImportError: html5lib not found, please install it
代码示例
# Python program to scrape table from website # import libraries selenium and time from selenium import webdriver from selenium.webdriver.edge.service import Service as EdgeService from webdriver_manager.microsoft import EdgeChromiumDriverManager from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.webdriver.edge.options import Options from time import sleep import pandas as pd URL = "https://open.bymadata.com.ar/#/negociable-obligations" # Create webdriver object # driver = webdriver.Chrome() options = Options() # selenium 4 driver = webdriver.Edge(service=EdgeService( EdgeChromiumDriverManager().install()), options=options) df = pd.read_html(driver.page_source) print(df[0])
Conda环境已安装包(关键片段)
# packages in environment at C:\Users\armartinof\.conda\envs\converter: # # Name Version Build Channel ... html5lib 1.1 pyhd3eb1b0_0 ... pandas 2.1.2 pypi_0 pypi ...
解决方案
1. 确认Python环境匹配
- 在代码开头添加以下代码,输出当前Python解释器路径:
import sys print(sys.executable) - 检查输出路径是否为
C:\Users\armartinof\.conda\envs\converter\python.exe,若不是,打开终端切换到对应Conda环境:
再运行脚本。conda activate converter
2. 重新安装html5lib及依赖
- 先卸载现有html5lib:
pip uninstall html5lib -y - 用Conda重新安装(确保依赖完整):
或用pip安装:conda install html5lib=1.1 -c conda-forgepip install html5lib==1.1
3. 强制指定解析器
修改read_html调用,显式指定使用html5lib解析器,避免pandas自动选择其他缺失依赖的解析器:
df = pd.read_html(driver.page_source, flavor='html5lib')
4. 修复代码逻辑
原代码未访问目标URL,需添加页面访问与加载等待,否则driver.page_source为空:
driver.get(URL) sleep(5) # 根据页面加载速度调整等待时间,也可使用Selenium显式等待 df = pd.read_html(driver.page_source, flavor='html5lib')
5. 验证webencodings依赖
html5lib依赖webencodings库,重新安装确保完整性:
pip install webencodings --upgrade
内容的提问来源于stack exchange,提问作者Nando7789
相关产品推荐
相关产品推荐

