BeautifulSoup导入错误

gma*_*ter 9 python beautifulsoup web-scraping

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

from bs4 import BeautifulSoup
Run Code Online (Sandbox Code Playgroud)

但我得到错误:

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

我尝试过各种方式安装BS4.首先,我做了

sudo pip install BeautifulSoup4
Run Code Online (Sandbox Code Playgroud)

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

sudo python setup.py install
Run Code Online (Sandbox Code Playgroud)

最后我试了一下

sudo su
easy_install BeautifulSoup4
Run Code Online (Sandbox Code Playgroud)

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

打字

pip freeze
Run Code Online (Sandbox Code Playgroud)

显示bs4已安装但正在运行

$ python3 -i
>>> help('modules')
Run Code Online (Sandbox Code Playgroud)

似乎没有列出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
Run Code Online (Sandbox Code Playgroud)

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

sudo pip3 install BeautifulSoup
Run Code Online (Sandbox Code Playgroud)

代替

sudo pip3 install BeautifulSoup4
Run Code Online (Sandbox Code Playgroud)

哪个有效.谢谢!

小智 12

解决方案是:

sudo pip3 install BeautifulSoup4
Run Code Online (Sandbox Code Playgroud)