导入错误:找不到名为BeautifulSoup的模块

98
我使用easy_install安装了BeautifulSoup,并尝试运行以下脚本。
from BeautifulSoup import BeautifulSoup
import re

doc = ['<html><head><title>Page title</title></head>',
       '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
       '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
       '</html>']
soup = BeautifulSoup(''.join(doc))

print soup.prettify()

但是不确定为什么会发生这种情况

Traceback (most recent call last):
  File "C:\Python27\reading and writing xml file from web1.py", line 49, in <module>
    from BeautifulSoup import BeautifulSoup
ImportError: No module named BeautifulSoup

请问你能帮个忙吗?谢谢!


你是否设置了 easy_install 来使用 Python2.7,或者使用了随操作系统自带的 Python 版本?如果没有指定 2.7,请尝试使用 Python2.4 或 Python2.6。 - TyrantWave
看起来你安装的BeautifulSoup是针对不同于你用来运行此脚本的Python版本的。 - Noufal Ibrahim
1
谢谢您的回复...是的,实际上我需要64位Windows的easy_install,但我安装了32位的。这里没有64位的easy_install:http://pypi.python.org/pypi/setuptools#files。那我该怎么办呢? - Muhammad Imran
10个回答

258

尝试使用 from bs4 import BeautifulSoup

这可能是与Beautiful Soup 4版本和beta版本相关的问题。我刚从主页上读到这个。


2
一年多过去了,什么也没变 :) - Corvin
2
对于未来的用户:截至2014年5月2日,此解决方案仍可在Windows 8.1上使用Python 3.4.0和BeautifulSoup 4.3.2。 - shermanzach
1
同样适用于Windows10 :) - user4398985
这个解决方案在Google Colab上也适用。其他的选择有问题。在找到这个之前需要稍微找一下。 - marcogemaque

22
在 Ubuntu 14.04 上,我通过 apt-get 安装了它,并且它运行良好: sudo apt-get install python-beautifulsoup 然后只需执行以下操作: from BeautifulSoup import BeautifulSoup

1
OP正在使用Windows操作系统(请注意他的回溯中的C:\Python27路径)。这个答案对他没有用。 - rmunn
2
@rmunn 那么那些使用Linux的用户(比如我)呢?我已经编辑了答案。感谢你的贡献... - Caumons

11

试一下这个方法,我的方式有效。想要获取任何标签的数据,只需将字母 "a" 替换为您想要的标签即可。

from bs4 import BeautifulSoup as bs
import urllib

url="http://currentaffairs.gktoday.in/month/current-affairs-january-2015"

soup = bs(urllib.urlopen(url))
for link in soup.findAll('a'):
        print link.string

6

您可以导入bs4而不是BeautifulSoup。 由于bs4是一个内置模块,因此不需要额外安装。

from bs4 import BeautifulSoup
import re

doc = ['<html><head><title>Page title</title></head>',
       '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
       '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
       '</html>']
soup = BeautifulSoup(''.join(doc))

print soup.prettify()

如果您想发出请求,可以使用 requests 模块。 request 使用 urllib 和 requests 模块。 但我个人建议使用 requests 模块而不是 urllib。
使用时需要安装模块:
$ pip install requests

以下是如何使用 requests 模块的方法:
import requests as rq
res = rq.get('http://www.example.com')

print(res.content)
print(res.status_code)

2
bs4 不是一个内置模块。 - wim

2

首先安装Beautiful Soup 4版本。在终端窗口中输入以下命令:

pip install beautifulsoup4

然后导入BeautifulSoup库。

无法运行。 - muhammad tayyab

0

我在Windows 10上使用Eclipse时遇到了同样的问题。

我按照推荐的方式通过Windows命令窗口(cmd)安装它:

C:\Users\NAMEOFUSER\AppData\Local\Programs\Python\beautifulsoup4-4.8.2\setup.py install 

BeautifulSoup是这样在我的Python目录中安装的:

C:\Users\NAMEOFUSE\AppData\Local\Programs\Python\Python38\Lib\site-packages\beautifulsoup4-4.8.2-py3.8.egg

在将bs4和EGG-INFO文件夹手动复制到site-packages文件夹后,一切都开始正常工作了,包括示例:

from bs4 import BeautifulSoup


html = """
    <html>
        <body>
            <p> Ich bin ein Absatz!</p>
        </body>
    </html>
"""
print(html)


soup = BeautifulSoup(html, 'html.parser')

print(soup.find_all("p"))

0

如果你有两个版本的Python,也许我的情况可以帮到你。

这是我的情况:

1-> Mac OSX

2-> 我有两个版本的Python,(1) 系统默认版本2.7 (2) 手动安装版本3.6

3-> 我使用sudo pip install beautifulsoup4安装了beautifulsoup4模块。

4-> 我使用python3 /XXX/XX/XX.py运行Python文件。

所以情况3和4是关键部分。我使用"pip"安装了beautifulsoup4模块,但是该模块是为Python 2.7版本安装的,而我使用"python3"运行Python文件。因此,你应该为Python 3.6安装beautifulsoup4模块。

使用sudo pip3 install beautifulsoup4,你可以为Python 3.6安装该模块。


0

如果您是以这种方式安装的(如果没有,就按照这种方式进行安装):

pip install beautifulsoup4

如果您使用了这段代码(如果没有,请使用此代码):

from bs4 import BeautifulSoup

如果您使用的是Windows系统,请检查是否有模块,可能保存在不同的路径中。

0

不要忘记检查这个 - 这可能是因为您的系统上安装了多个 Python 版本,并且该库正在安装到不同的版本上,因此无法在您尝试使用的特定版本上访问。我花了很多时间陷入这个问题中,但提供的解决方案都没有起作用。


-1
尝试这个: 从bs4导入BeautifulSoup

这只是最受欢迎答案的部分副本,没有格式或上下文。请不要抄袭答案。 - Chris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接