2014-09-22 36 views
6
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')) 

错误,我越来越:如何解决AttributeError的: 'NoneType' 对象在Python中没有属性 '编码'

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

什么打印con.string给你 – 2014-09-22 06:44:16

回答

5

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   
+0

@dav idism转换空字符串没有意义吧? 。所以对于这两件事情来说,它可能是有效的,而不是给予 – 2014-09-22 06:52:09

相关问题