python - BeautifulSoup 导入错误

标签 python web-scraping beautifulsoup

我正在尝试编写一个简单的本地 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

有效。谢谢!

最佳答案

解决方法是:

sudo pip3 install BeautifulSoup4

关于python - BeautifulSoup 导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28637314/

相关文章:

node.js - 使用node.io抓取时如何添加像Tor这样的代理?

python - 将 BeautifulSoup 中的文本写入文件

python - beautifulsoup 只提取前 10 个元素

python - 是否可以用 beautifulsoup 抓取 "dynamical webpage"?

python - 如何使用 python pandas 读取已经打开的 excel 表

python - geopy.distance.geodesic() 但它说 TypeError : unsupported operand type(s) for +=: 'int' and 'tuple'

python - Selenium 点击错误

python - 如何在网站上抓取嵌入的整数

Python Lambdas 和变量绑定(bind)

python - 抓取网站并仅将可见文本导出到文本文档 Python 3 (Beautiful Soup)