使用python的youtube视频评论下载程序

使用python的youtube视频评论下载程序,python,Python,“youtube视频评论是使用json文件获取的” “这将获取一个python对象并将其转储到一个JSON字符串中 “该对象的表示” 获取错误:TypeError:需要类似字节的对象,而不是'str' TypeError回溯(最近一次调用) 在() 32 results=(result[“items”][i].get('snippet').get(“topLevelComment”).get('snippet').get(“textDisplay”).encode(“utf-8”) 33打印(结

“youtube视频评论是使用json文件获取的”

“这将获取一个python对象并将其转储到一个JSON字符串中 “该对象的表示”

获取错误:TypeError:需要类似字节的对象,而不是'str'

TypeError回溯(最近一次调用)
在()
32 results=(result[“items”][i].get('snippet').get(“topLevelComment”).get('snippet').get(“textDisplay”).encode(“utf-8”)
33打印(结果)
--->34结果=结果。替换(“,”,“”)
35#打印(结果[“项”][i].get('snippet').get('topLevelComment').get('snippet').get('textDisplay')).encode(“utf-8”)
36#writer.writerow((结果[“项”][i].get('snippet').get('topLevelComment').get('snippet').get('textDisplay')).encode('utf-8'))
TypeError:需要类似字节的对象,而不是“str”
这里的责任在于最后一部分
.encode(“utf-8”)
,它将字符串转换为字节,这很好,除非您尝试使用
替换
,使用常规字符串。建议(任何最适合你的):

选项1如果可以,只需将该零件从生产线中移除即可

results = result["items"][i].get('snippet').get("topLevelComment").get('snippet').get("textDisplay")
选项2在尝试
替换之前添加
解码

results = results.decode().replace(",", "")
选项3使用适当的字节替换

results = results.replace(b",", b"")

选项1是理想的选项,因为它更简单,并且与代码的其余部分更兼容(首先不需要转换为字节,我看不出它有什么作用)

结果显然最终必须是utf-8,因此选项1和2应该有一个
encode('utf-8'))
替换
调用之后。我希望是这样,是的,或者在它无法写入csv文件后使用选项3,给出错误“位置20-23中的字符:选项3之后的字符映射到”@ADITYA?如果是这样的话,请为您提出另一个问题that@OferSadan谢谢你的rply。请通过问题链接尝试
结果。解码('utf-8')。替换…
,同时将注释放入excel文件中。在位置20-23:字符映射到中给出错误字符
results= (result["items"][i].get('snippet').get("topLevelComment").get('snippet').get("textDisplay")).encode("utf-8")
results = result["items"][i].get('snippet').get("topLevelComment").get('snippet').get("textDisplay")
results = results.decode().replace(",", "")
results = results.replace(b",", b"")