2017-08-13 35 views
1

我的腳本寫在下面,並且找到soup.get_text()命令的錯誤。 代碼:BeautifulSoup:解析HTML文件時無類型錯誤

from BeautifulSoup import * 
soup=BeautifulSoup(open("F:\\HTML\\Registrationform.html")) 
print soup.get_text('+') 

錯誤:文件 「C:/Python27/beautifulsoup4-4.6.0.tar/scrapingbasic.py」,3號線,在

print soup.get_text('+') 
TypeError: 'NoneType' object is not callable 
+0

更新爲[beautifulsoup4](https://www.crummy.com/software/BeautifulSoup/bs4/doc/) – SmartManoj

回答

1

BeautifulSoup類希望在構造html/xml內容。所以加.read()到你的open函數應該可以。 下面的代碼:

from BeautifulSoup import * 
soup=BeautifulSoup(open("F:\\HTML\\Registrationform.html").read()) 

print soup.get_text('+') 

另外,我建議你升級到BeautifulSoup4

希望這會有所幫助。

0

Beautifulsoup希望html/xml文檔。你能檢查一下,如果python 2.x能解析你的html文件,只是爲了重新檢查。另一個問題可能發生在Windows上,需要確保lxml庫安裝成功。用下面的部分https://www.crummy.com/software/BeautifulSoup/bs4/doc/

:也可以重新檢查從文檔

from bs4 import BeautifulSoup 
with open("index.html") as fp: 
    soup = BeautifulSoup(fp) 
+0

運行成功打印soup.get_text('+')。 謝謝大家。 –