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

2024-03-29 09:51:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经使用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

你能帮忙吗。 谢谢


Tags: fromimportiddoctitleishtmleasy
3条回答

试试这个,我的工作方式是这样的。要获取任何标记的数据,只需将“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

在Ubuntu 14.04上,我从apt-get安装了它,运行良好:

sudo apt-get install python-beautifulsoup

那就做:

from BeautifulSoup import BeautifulSoup

试试这个from bs4 import BeautifulSoup

这可能是美丽汤的问题,版本4,和测试日。我刚从主页上看到这个。

相关问题 更多 >