如何解决AttributeError:'NoneType'对象在python中没有属性'encode'

时间:2014-09-22 06:37:52

标签: python file beautifulsoup

for comment_entry in comment_feed.entry:
content = comment_entry.ToString()
parse = BeautifulSoup(content)
for con in parse.find('ns0:content'):
    print con.string
    s = con.string
    file.write(s.encode('utf8'))

我得到的错误:

File "channel_search.py", line 108, in youtube_search
file.write(s.encode('utf8'))
AttributeError: 'NoneType' object has no attribute 'encode'

1 个答案:

答案 0 :(得分:11)

您的s可能是 Nonetype

尝试

s = con.string
if s:file.write(s.encode('utf8'))
# or if s is not None        
#if you want to check only for None