2016-04-24 118 views
1

我想让美女跑在我的Mac上跑,它不工作,我在我的智慧结束。它似乎很容易安装,这更令人生气。让美女跑跑蟒蛇

我运行Python版本3.5.1

我Python的新手 - 花了最后一小时通过Python文档和其他堆栈溢出的工作,但还没有找到工作的解决方案呢。

下面的代码(需要在SYS路径线,因为这就是它安装的时候我跑PIP安装beautifulsoup4)

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()) 

这是我得到的错误:

TypeError: 'module' object is not callable 

大。因此,我发现了一些建议,说:“您正在导入包含类,函数等的模块。为了实例化BeautifulSoup类实例形成BeautifulSoup模块,您需要导入它或使用全名,包括像yonili这样的模块前缀建议在评论上述”

What does "module object is not callable" mean?

所以我改变了代码如下:

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' 
+0

我不认为第一行代码是必要的,除非你以某种方式安装python软件包到您的用户文件夹 –

+0

您是否安装了beautifulsoup4'? –

+0

[Here](http://stackoverflow.com/a/3368295/5299236),但我建议将您的BeautifulSoup更新为4并执行下面的操作无论如何回答。 –

回答

3

可以导入这样的:

from BeautifulSoup import BeautifulSoup 

如果使用BeautifulSoup 4,您需要导入这样的:此外

from bs4 import BeautifulSoup 

,您可以使用import * as *

from bs4 import BeautifulSoup as somename 
+0

我只是要编辑我的问题。当我这样做时,我得到这个错误:ImportError:没有名为'bs4'的模块 – CBA

+0

我检查了pip冻结,它说beautifulsoup4 == 4.4.1 – CBA

+0

只是做了pip安装bs4。现在pip freeze说bs4 == 0.0.1。但仍然没有得到模块命名bs4错误 – CBA