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

Jupyter Notebook导入BeautifulSoup4遇ImportError与ValueError问题咨询

问题解决指南

问题症状

在Jupyter Notebook中安装BeautifulSoup4后,运行下述代码时触发两个错误:

from selenium.webdriver import Chrome    
from selenium.webdriver.chrome.service import Service  
from selenium.webdriver.chrome.options import Options  
from bs4 import BeautifulSoup  
import pandas as pd

ImportError: 'You are trying to use a Python 3-specific version of Beautiful Soup under Python 2. This will not work. The final version of Beautiful Soup to support Python 2 was 4.9.3.'
ValueError: source code string cannot contain null bytes


分步解决

1. 处理Python版本不兼容问题

  • 先确认当前Jupyter内核的Python版本:在Notebook单元格中运行以下代码查看:
    import sys
    print(sys.version)
    
  • 如果显示为Python 2.x:
    • 卸载现有BeautifulSoup4,安装适配Python 2的最后稳定版:
      pip uninstall beautifulsoup4 -y
      pip install beautifulsoup4==4.9.3
      
    • 长期方案建议切换到Python 3环境:创建Python 3虚拟环境,重新安装selenium、beautifulsoup4、pandas,再在Jupyter中切换到该虚拟环境对应的内核。

2. 处理源码含空字节问题

  • 检查Notebook文件是否损坏:用文本编辑器打开.ipynb文件,排查是否存在不可见的异常字符,删除后重新保存。
  • 尝试重新安装所有相关依赖包,覆盖可能损坏的文件:
    pip uninstall selenium beautifulsoup4 pandas -y
    pip install selenium beautifulsoup4 pandas
    
  • 新建空白Notebook,手动复制代码(避免复制隐藏异常字符),再运行测试。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 19:01:14