ImportError:没有名为BeautifulSoup的模块

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

能否请你帮忙。 谢谢

from bs4 import BeautifulSoup试试这个

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

在Ubuntu 14.04上,我从apt-get安装了它,它工作正常:

sudo apt-get install python-beautifulsoup

然后只是做:

from BeautifulSoup import BeautifulSoup

试试这个,我的工作是这样的。 要获取标签的任何数据,只需用你想要的标签replace“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 

你可以导入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() 

如果你想请求,使用请求模块。 请求正在使用urllibrequests模块。 但我个人推荐使用requests模块,而不是urllib

模块安装使用:

 $ pip install requests 

以下是如何使用请求模块:

 import requests as rq res = rq.get('http://www.example.com') print(res.content) print(res.status_code) 

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

这是我的情况

1-> mac osx

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

3>我已经用sudo pip install beautifulsoup4

4->我用python3 /XXX/XX/XX.py运行python文件

所以这种情况3和4是关键的一部分,我已经安装beautifulsoup4与“点”,但这个模块安装了python版本2.7,我运行与“python3”的Python文件。 所以你应该为python 3.6安装beautifulsoup4;

sudo pip3 install beautifulsoup4你可以安装python 3.6的模块