问题描述
|
嗨,我正在运行python 2.7.1和beautifulsoup 3.2.0
如果我尝试使用加载一些xml Feed
ifile = open(os.path.join(self.path,str(self.Feed_ID)+\'.xml\'),\'r\')
file_data = BeautifulStonesoup(ifile,convertEntities=BeautifulStonesoup.XHTML_ENTITIES)
即时通讯收到以下错误
File \"C:\\dev\\Python27\\lib\\site-packages\\BeautifulSoup.py\",line 1144,in __ini
t__
self._Feed(isHTML=isHTML)
File \"C:\\dev\\Python27\\lib\\site-packages\\BeautifulSoup.py\",line 1186,in _Feed
SGMLParser.Feed(self,markup)
File \"C:\\dev\\Python27\\lib\\sgmllib.py\",line 103,in Feed
self.rawdata = self.rawdata + data
TypeError: cannot concatenate \'str\' and \'nonetype\' objects
我尝试环顾四周,但没有成功...请指教
解决方法
举个例子...
from BeautifulSoup import BeautifulStoneSoup
xml = \"<doc><tag1>Contents 1<tag2>Contents 2<tag1>Contents 3\"
soup = BeautifulStoneSoup(xml)
print soup.prettify()
(...)
从这里。我推断您需要将字符串作为第一个参数而不是文件对象ifile
传递,请尝试:
file_data = BeautifulStoneSoup(ifile.read(),convertEntities=BeautifulStoneSoup.XHTML_ENTITIES)
,我也有这个错误。这对我有用:
from unidecode import unidecode
file_data = BeautifulSoup(unidecode(ifile.read()))