我使用以下命令安装了 BeautifulSoup:
sudo easy_install BeautifulSoup4
我收到消息:
Searching for BeautifulSoup4
Best match: beautifulsoup4 4.1.3
Processing beautifulsoup4-4.1.3-py2.6.egg
beautifulsoup4 4.1.3 is already the active version in easy-install.pth
Using /Library/Python/2.6/site-packages/beautifulsoup4-4.1.3-py2.6.egg
Processing dependencies for BeautifulSoup4
Finished processing dependencies for BeautifulSoup4
我正在尝试导入 BeautifulSoup 库。
>>> from BeautifulSoup import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named BeautifulSoup
或:
>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named bs4
我的Python版本是:
python --version
Python 2.7.3
编辑
我明白了:
Using /Library/Python/2.6/site-packages/beautifulsoup4-4.1.3-py2.6.egg
可能意味着 Python 版本之间存在冲突
如何注册此模块?谢谢
应该是,
from bs4 import BeautifulSoup
这样做:
easy_install pip
pip install beautifulsoup4
。这应该有效。确保您将其作为带有
pip list
的模块,如果您看到 Beautiful Soup 作为输出,那么是的,您已经可以正常工作了。
经过一番研究,我发现这可以解决问题:
pip uninstall BeautifulSoup4
将卸载以下位置的软件包:
/Library/Python/2.6/site-packages/
并且:
easy_install-2.7 BeautifulSoup4
将成功安装软件包:
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/
我还检查了其他一些具有相同问题的软件包的过程,并且它有效。
我来晚了一点,但是我 pip 安装了 beautiful soup 并且还下载了 bs4 文件夹。 Python 给我一个回溯说明:第 2 行, 从 bs4 导入 BeautifulSoup 导入错误:无法从“bs4”导入名称“BeautifulSoup”
你也必须
pip install bs4
并使用
>>> from bs4 import BeautifulSoup