MacOS下Python爬虫遇ImportError:找不到BeautifulSoup/BeautifulSoup4模块
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
pip3instead ofpipto 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

