导入错误:没有名为BeautifulSoup的模块

100 投票
10 回答
147080 浏览
提问于 2025-04-16 15:44

我用 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

10 个回答

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
22

在Ubuntu 14.04上,我通过apt-get安装了它,运行得很好:

sudo apt-get install python-beautifulsoup

然后只需要这样做:

from BeautifulSoup import BeautifulSoup

260

试试这个 from bs4 import BeautifulSoup

这可能是和Beautiful Soup 4版本有关的问题,还是在测试阶段的时候。我刚从官网上看到的。

撰写回答