BeautifulSoup无法连接str和NoneType对象

时间:2011-05-24 12:42:15

标签: python beautifulsoup

嗨我正在运行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

我试着到处寻找但没有成功......请告知

2 个答案:

答案 0 :(得分:0)

以示例......

from BeautifulSoup import BeautifulStoneSoup
xml = "<doc><tag1>Contents 1<tag2>Contents 2<tag1>Contents 3"
soup = BeautifulStoneSoup(xml)
print soup.prettify()
(...)
来自here

。我推断你需要传递一个字符串作为第一个参数而不是文件对象ifile,尝试:

file_data = BeautifulStoneSoup(ifile.read(), 
    convertEntities=BeautifulStoneSoup.XHTML_ENTITIES)

答案 1 :(得分:0)

我也有这个错误。这对我有用:

from unidecode import unidecode
file_data = BeautifulSoup(unidecode(ifile.read()))