简体   繁体   中英

BeautifulSoup cannot concatenate str and NoneType objects

hi im running python 2.7.1 and beautifulsoup 3.2.0 if i try to load some xml feed using

ifile = open(os.path.join(self.path,str(self.FEED_ID)+'.xml'), 'r')
file_data = BeautifulStoneSoup(ifile, 
    convertEntities=BeautifulStoneSoup.XHTML_ENTITIES)

im getting the following error

  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

i try to look everywhere but with no success... please advise

I had this error too. This worked for me:

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

With the example...

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

from here . I infer that you need to pass a string as first parameter instead of the file object ifile , try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM