2017-08-13 60 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('+')。 谢谢大家。 –

相关问题