python - 如何解决 AttributeError : 'NoneType' object has no attribute 'encode' in python

标签 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'

最佳答案

您的 可能是 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          

关于python - 如何解决 AttributeError : 'NoneType' object has no attribute 'encode' in python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25967959/

相关文章:

Python 在函数中使用 Lambda

c# - 在标签文本中显示文件名

python - 如果没有数据,尝试使用 bs4 跳过属性

python - 类型错误 : unhashable type: 'dict' in Networkx random walk code that was previously working

python - 使用 Poetry 自动从 Git 安装 Python 依赖项

file - 检查目录中是否存在多个文件

Python 在写入文件时修改换行符

Python 从 Html 代码中抓取

python - 使用python urllib和beautiful soup从html站点中提取信息

python - 如何在 BeautifulSoup4 中抓取结束标签旁边的文本?