我用PRAW构建了一个非常简单的脚本,它可以在redid.com/r/worldnews上打印前10个链接标题。我希望这可以与GeekTool一起使用,但只有以下几个方面显示出来:
“红迪网上十大新闻
1个新闻标题
2“
我不知道为什么会发生这种情况,因为当直接从命令行运行脚本时,我没有任何问题。
下面是python脚本:
import praw
def main():
subreddit = r.get_subreddit('worldnews')
x = 1
print "TOP 10 NEWS ON REDDIT"
print ''
for submission in subreddit.get_hot(limit=10):
print x, submission.title
x = x+1
print ' '
if __name__ == "__main__":
user_agent = "Top10 0.1 by /u/alexisfg"
r = praw.Reddit(user_agent=user_agent)
main()发布于 2014-08-12 11:56:52
如果在主函数周围放置一个try...except以打印任何异常,则会得到以下错误消息:
ascii codec can't encode character u'\u2019' in position 12: ordinal not in range(128)因此,这是一个编码问题--第二个标题中的某些字符不在ASCII范围内,python/Geektool正在使用它作为默认编码。您可以通过使用.encode('utf-8')显式编码标题字符串来解决这个问题。
https://stackoverflow.com/questions/25263320
复制相似问题