首页 > 解决方案 > 为什么我的带有 PRAW 的 Reddit 机器人返回意外的字母字符串?

问题描述

我正在开发一个 Reddit 机器人,其目标是确定特朗普在流行的政治子版块中被提及的频率。

我一直在关注一些教程,为了清楚起见,我将它们链接在这里:

该机器人大部分时间都在工作。这是代码:

#TrumpSpamBot
import praw
# in order to use our script, we need 4 pieces of information
# client_id:
#client_secret:
#username:
#password:

#create the reddit variable, assigning it 5 paramaters.
reddit = praw.Reddit(client_id="----",
client_secret="----",
password= "----",
user_agent="A script by /u/----",
username="TrumpSpamBot")

#displays which scopes are available on the Reddit instance
print(reddit.auth.scopes())

#checking that the above code works properly.
print(reddit.user.me())

#declaring what subreddits my bot will be living on.
subreddit = reddit.subreddit("politics")


#declaring what post we are going to be looking at.
submission = reddit.submission(id="glikt4")

for top_level_comment in submission.comments:
    print(top_level_comment.body)

我审查了所有个人信息,例如密码和姓名。此阶段的代码应该返回机器人的名称“TrumpSpamBot”,然后进入特定的 Reddit 帖子,并开始列出所有顶级评论。目前,它列出了一种,然后吐出一堆看似随机的字母组合。结果可以在这里看到:

[Command: python -u 'C:\Users\Trevor\Desktop\Reddit Bot\RedditBotTest1.py']
{'*'}
TrumpSpamBot
fqzckq5
fqzcl22
fqzclcg
As a reminder, this subreddit [is for civil discussion.](/r/politics/wiki/index#wiki_be_civil)

In general, be courteous to others. Debate/discuss/argue the merits of ideas, don't attack people. Personal insults, shill or troll accusations, hate speech, **any** advocating or wishing death/physical harm, and other rule violations can result in a permanent ban. 

If you see comments in violation of our rules, please report them.

 For those who have questions regarding any media outlets being posted on this subreddit, please click [here](https://www.reddit.com/r/politics/wiki/whitelist) to review our details as to whitelist and outlet criteria.

***


*I am a bot, and this action was performed automatically. Please [contact the moderators of this subreddit](/message/compose/?to=/r/politics) if you have any questions or concerns.*
It drives me insane that the current administration STILL refuses to do something. From a completely self serving perspective, managing the US COVID response would have been the perfect way to set up for re-election. You could rally the entire country behind what should be a truly non partisan issue. But no, they just had to politicize this because it is so much easier to blame someone else and do nothing than actually take some action.
Traceback (most recent call last):
  File "C:\Users\Trevor\Desktop\Reddit Bot\RedditBotTest1.py", line 30, in <module>
    print(top_level_comment.body)
  File "C:\Python38\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 16-19: character maps to <undefined>
[Finished in 3.494s]fqzcl06
fqzcl22
fqzcl3h
fqzcl06
fqzcl3h
fqzcljd
fqzclk7
fqzcljd
fqzclk7
fqzclne
fqzclne
fqzclsc

我不知道这些字母应该代表什么。我尝试注释掉代码的特定部分,看看我是否能确定是什么原因造成的。我让它只用第 2 行和第 10 行产生了类似的结果。所以这两行代码的某些东西发送了所有这些奇怪的字母组合。这应该发生吗?

标签: pythonreddit

解决方案


我对这种错误不是很熟悉,但我认为这是编码的问题。标准是utf-8,我做了一些研究:

尝试添加.encode("utf.8")您的打印件:

print(reddit.auth.scopes().encode("utf-8"))

如果它不起作用,您也可以尝试添加 encoding="utf-8" 作为您收到错误的参数(我真的看不出它在哪里)

UnicodeEncodeError:“charmap”编解码器无法编码字符

python 3.2 UnicodeEncodeError:“charmap”编解码器无法在位置 9629 编码字符“\u2013”​​:字符映射到 <undefined>


推荐阅读