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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25967959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:53:19  来源:igfitidea点击:

How to resolve AttributeError: 'NoneType' object has no attribute 'encode' in python

pythonfilebeautifulsoup

提问by user3844662

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'))

Error which I'm getting:

我得到的错误:

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

采纳答案by sundar nataraj

Your smight be Nonetype

s可能是Nonetype

Try

尝试

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