2015-12-29 71 views
1

我試圖讓使用Python的BS4蜘蛛,我已經使用或者PIP和easy_install的已安裝BS4,但一旦我運行使用PyCharm程序,它提供了一個錯誤:PyCharm不能導入BeautifulSoup

Traceback (most recent call last): 
    File "C:/PyCharm Project/bs4.py", line 3, in <module> 
    from bs4 import BeautifulSoup 
    File "C:\PyCharm Project\bs4.py", line 3, in <module> 
    from bs4 import BeautifulSoup 
ImportError: cannot import name 'BeautifulSoup' 

Process finished with exit code 1 

但在命令提示符這個錯誤不會出現:

C:\WINDOWS\system32>python 
Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:16:59) [MSC v.1900 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from bs4 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> 
... """ 
>>> 
>>> # create a bs object using a HTML page 
... soup = BeautifulSoup(html_doc, 'html.parser', from_encoding='utf-8') 
>>> soup.title 
<title>The Dormouse's story</title> 
>>> soup.title.string 
"The Dormouse's story" 
>>> 

項目解釋:

+1

在PyCharm中使用了什麼解釋器? –

+0

@tales_padua 3.5.0 python –

回答

5

你叫你的腳本C:/PyCharm Project/bs4.py",與from bs4 import BeautifulSoup你實際上試圖我從您自己的腳本中輸入BeautifulSoup而不是bs4庫,因此您需要重命名並刪除目錄中的任何.pyc文件。

+1

是的,這是問題!我將python文件重命名爲'test.py',現在它可以工作了!謝謝! –

0

機會pycharm是CONFI gured使用python2.7 ......你可以在文件interpretter

import bs4你的文件的頂部更改到3.5>設置>項目設置> ...當它與紅色下劃線,移動光標在它下面,然後點擊alt + enter,然後選擇「install package bs4」

+0

解釋器是Python 3.5作爲我發佈的圖像;我已經安裝了bs4,因此PyCharm不會用紅色強調'import bs4',事實上,當我執行'print bs4'時,它會打印出bs4 lib路徑。 –

0

您應該再次安裝它,或者您應該將解釋器更改爲python 3.5或在文件頂部鍵入import bs4

+0

我已經安裝好幾次了,它不會改變任何東西。 –

+0

我使用了「import bs4.BeautifulSoup」和「from bs4 import BeautifulSoup」,它給出了相同的錯誤。解釋器是python 3.5.0。 –