【问题标题】:BeautifulSoup import errorBeautifulSoup 导入错误
【发布时间】:2015-04-22 14:58:29
【问题描述】:

我正在尝试编写一个简单的本地 python 脚本来进行一些 html 解析。我安装了 beautifulsoup4 并使用

导入了它
from bs4 import BeautifulSoup

但我得到了错误:

Traceback (most recent call last):
  File "scrape_descriptions.py", line 1, in <module>
    from bs4 import BeautifulSoup
ImportError: No module named 'bs4'

我几乎尝试过以各种方式安装 BS4。首先我做了

sudo pip install BeautifulSoup4

然后我尝试从网站下载实际文件并运行

sudo python setup.py install

最后我尝试了

sudo su
easy_install BeautifulSoup4

所有这些操作似乎都已成功完成。但我仍然收到此错误。我搜索了其他帖子,但几乎所有帖子都只是安装说明,我已经完成了。

打字

pip freeze

显示 bs4 已安装但正在运行

$ python3 -i
>>> help('modules')

似乎没有列出 bs4。

编辑 1:运行 sudo pip3 install BeautifulSoup 会出现此错误:

Downloading/unpacking BeautifulSoup
  Downloading BeautifulSoup-3.2.1.tar.gz
  Running setup.py (path:/private/tmp/pip_build_root/BeautifulSoup/setup.py) egg_info for package BeautifulSoup
    Traceback (most recent call last):
      File "<string>", line 17, in <module>
      File "/private/tmp/pip_build_root/BeautifulSoup/setup.py", line 22
        print "Unit tests have failed!"
                                      ^
    SyntaxError: invalid syntax
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):

  File "<string>", line 17, in <module>

  File "/private/tmp/pip_build_root/BeautifulSoup/setup.py", line 22

    print "Unit tests have failed!"

                                  ^

SyntaxError: invalid syntax

----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /private/tmp/pip_build_root/BeautifulSoup
Storing debug log for failure in /Users/griff/.pip/pip.log

编辑 2:已解决! pip3 失败的原因是因为我在使用

sudo pip3 install BeautifulSoup

而不是

sudo pip3 install BeautifulSoup4

这很有效。谢谢!

【问题讨论】:

  • 如果您同时安装了 python 2 和 3,并为两者都安装了 pip,您可能是在安装一个并使用另一个。检查您是否有pip2pip3 命令,并尝试运行python2 并在那里进行导入。
  • 我确实有 pip2 和 pip3,但我只有 python3,没有 python2。
  • 然后运行pip3 install BeautifulSoup4
  • 导致错误,如上所列。
  • 啊。不知何故,你的点子很奇怪。您可能希望重新安装 pip,方法是下载他们的引导程序 .py 文件并专门使用 python3 运行它。之后,pip3 install BeautifulSoup4 应该可以工作了。 (如果不是,它可能是奇怪的 Python 安装目录的功能,在这种情况下,答案取决于系统。)

标签: python web-scraping beautifulsoup


【解决方案1】:

解决办法是:

sudo pip3 install BeautifulSoup4

【讨论】:

  • 很好的解决方案@ers81239
【解决方案2】:

试试:

sudo pip install BeautifulSoup

然后

from BeautifulSoup import BeautifulSoup

您现在可以从 BeautifulSoup 调用所有函数

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多