2016-04-24 41 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