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

MacOS下Python爬虫遇ImportError:找不到BeautifulSoup/BeautifulSoup4模块

Fixing Your BeautifulSoup4 Script Error on macOS

Hey there, let's work through this error you're seeing after installing beautifulsoup4. Based on what you described, here are the most likely fixes to get your crawler running:

1. Update Your Script's Import Statement

The old BeautifulSoup package is long deprecated—what you installed is beautifulsoup4, which uses a different import path. If your scrape.py has lines like:

import BeautifulSoup

Or:

from BeautifulSoup import BeautifulSoup

You need to swap them for the beautifulsoup4 syntax:

from bs4 import BeautifulSoup

This is the #1 reason people hit errors after switching to the new package.

2. Align Your pip and Python Versions

macOS comes with an outdated pre-installed Python 2, and Homebrew probably set up Python 3 for you. If you used pip to install packages but run your script with the system's default python (which points to Python 2), the package won't be found.

  • Use pip3 instead of pip to install packages for Python 3:
    pip3 install beautifulsoup4 ipython
    
  • Run your script with Python 3 explicitly:
    python3 scrape.py
    

3. Verify the Package is Installed Correctly

To double-check that beautifulsoup4 lives where your script is looking, run this command to see the package's location:

pip3 show beautifulsoup4

Then test the import directly in a Python 3 shell:

python3
from bs4 import BeautifulSoup
# If no error pops up, the package is installed properly

4. Check for Virtual Environment Misalignment

If you're using a virtual environment (like venv or conda), make sure you activated it before installing packages. If you installed beautifulsoup4 outside the env but run the script inside it, the package won't be available. Activate your env first, then re-install the requirements:

# Example for venv
source your_env_name/bin/activate
pip install -r requirements.txt

Once you fix the import statement and align your pip/Python versions, your crawler should run without that traceback error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:50:46