获取 &而不是 &使用 PRAW 返回标题 [英] Getting & instead of & in title return using PRAW

查看:89
本文介绍了获取 &而不是 &使用 PRAW 返回标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PRAW 获得给定 subreddit 有史以来前 25 名:

I'm trying to get the top 25 of all time of a given subreddit using PRAW:

import praw
subreddit = 'gamedeals'
r = praw.Reddit(user_agent='getting top 25 of all time by /u/sqrg')
submissions = r.get_subreddit(subreddit).get_top_from_all(limit=25)
titlesFile = open("text.txt", 'w')
for s in submissions:
    titlesFile.write(s.title.encode('utf-8', 'replace') + '\n')
titlesFile.close()

我收到以下错误:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 63: ordinal not in range(128)

UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in position 63: ordinal not in range(128)

所以我将 for 循环内的行改为:

So I changed the line inside the for loop to:

titlesFile.write(s.title.encode('utf-8', 'replace') + '\n')

它可以工作,但在 text.txt 文件中,我得到的是 & 而不是 &.我可以用一些字符串替换功能来改变它们,但是有没有办法直接写出正确的标题?另外,为什么我必须使用 encode() 方法?

And it works, but in the text.txt file I get & instead of &. I could change them with some string replace function, but is there any way to directly write the correct title? Also, why did I have to use the encode() method?

推荐答案

启用解码 html 实体的设置:

Enable the setting to decode html entities:

r = praw.Reddit(user_agent='getting top 25 of all time by /u/sqrg')
r.config.decode_html_entities = True

配置文件文档:https://praw.readthedocs.org/en/最新/页面/configuration_files.html

更多信息在这里:https://github.com/praw-dev/praw/问题/186

这篇关于获取 &而不是 &使用 PRAW 返回标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆