让beautifulsoup为python运行

让beautifulsoup为python运行,python,beautifulsoup,Python,Beautifulsoup,我正试图让beautifulsoup在我的Mac上运行,但它不工作,我束手无策。它看起来真的很容易安装,这更让人恼火 我正在运行Python版本3.5.1 我是Python的新手-我花了最后一个小时研究Python文档和其他堆栈溢出,但还没有找到一个有效的解决方案 下面是sys path行所需的代码,因为当我运行pip install SUPP4时,它就是在这里安装的 import sys; sys.path.insert(0, "/Users/username/Library/Python/2

我正试图让beautifulsoup在我的Mac上运行,但它不工作,我束手无策。它看起来真的很容易安装,这更让人恼火

我正在运行Python版本3.5.1

我是Python的新手-我花了最后一个小时研究Python文档和其他堆栈溢出,但还没有找到一个有效的解决方案

下面是sys path行所需的代码,因为当我运行pip install SUPP4时,它就是在这里安装的

import sys; sys.path.insert(0, "/Users/username/Library/Python/2.7/lib/python/site-packages")

import beautifulsoup

html_doc = """
<html><head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>

<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>

<p class="story">...</p>
"""

soup = beautifulsoup(html_doc, 'html.parser')

print(soup.prettify())
太好了。因此,我发现一些建议说,您正在导入包含类、函数等的模块。为了从BeautifulSoup模块实例化BeautifulSoup类实例,您需要导入它或使用包含模块前缀的全名,如yonili在上述注释中建议的那样

因此,我将代码改为:

beautifulsoup.beautifulsoup(html_doc, 'html.parser')
现在我得到了这个错误

AttributeError: module 'beautifulsoup' has no attribute 'beautifulsoup'
我也试过了

from beautifulsoup import beautifulsoup
这给了我错误

ImportError: cannot import name 'beautifulsoup'
最后我试了一下

from bs4 import BeautifulSoup
我发现了错误

ImportError: No module named 'bs4'

您可以这样导入:

from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup
如果使用BeautifulSoup 4,则需要如下方式导入:

from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup
此外,您还可以使用import*作为*

e、 g


您可以这样导入:

from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup
如果使用BeautifulSoup 4,则需要如下方式导入:

from BeautifulSoup import BeautifulSoup
from bs4 import BeautifulSoup
此外,您还可以使用import*作为*

e、 g

为了达到以下目的,请使用:

然后,您可以将其导入为:

from bs4 import BeautifulSoup
为了达到以下目的,请使用:

然后,您可以将其导入为:

from bs4 import BeautifulSoup

我只是想编辑一下我的问题。当我这样做时,我得到了这个错误:ImportError:没有名为“bs4”的模块。我检查了pip冻结,它说beautifulsoup4==4.4.1 pip刚刚安装了bs4。现在,pipfreeze表示bs4==0.0.1。但是仍然没有名为bs4 Error的模块,我认为需要使用pip3,而beautifulsoup4是正确的包。我只是想编辑一下我的问题。当我这样做时,我得到了这个错误:ImportError:没有名为“bs4”的模块。我检查了pip冻结,它说beautifulsoup4==4.4.1 pip刚刚安装了bs4。现在,pipfreeze表示bs4==0.0.1。但是仍然没有名为bs4 Error的模块,我认为需要使用pip3,而beautifulsoup4是正确的包。我不认为第一行代码是必要的,除非您以某种方式将python包安装到您的用户文件夹中。您是否安装了beautifulsoup4?​​​​​​​​​​​​​​​, 但我建议将您的BeautifulSoup更新为4,并按照下面的答案执行。@cricket_007 yes行代码是必需的,因为即使我使用的是python 3,它也安装在python 2.7文件夹中。是的,我做了,你是怎么运行python的?如果它来自一个编辑器,那么编辑器的路径上可能有一个与pip不同的python。从运行pip(可能是终端)的地方,运行命令python-c'import sys;printsys.executable';在编辑器中,添加导入sys行和相同的printsys.executable行。这两种安装路径相同吗?我不认为第一行代码是必要的,除非您以某种方式将python包安装到您的用户文件夹中。您是否安装了beautifulsoup4?​​​​​​​​​​​​​​​, 但我建议将您的BeautifulSoup更新为4,并按照下面的答案执行。@cricket_007 yes行代码是必需的,因为即使我使用的是python 3,它也安装在python 2.7文件夹中。是的,我做了,你是怎么运行python的?如果它来自一个编辑器,那么编辑器的路径上可能有一个与pip不同的python。从运行pip(可能是终端)的地方,运行命令python-c'import sys;printsys.executable';在编辑器中,添加导入sys行和相同的printsys.executable行。两个安装路径是否相同?