为什么我的 Reddit 机器人出现 PRAW returns 意想不到的字符串?

Why is my Reddit bot with PRAW returns unexpected strings of letters?

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

我一直在学习一些教程,为了清楚起见,我将 link 放在这里:

该机器人在大部分情况下都能正常工作。这是代码:

#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)

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

[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 行就产生了类似的结果。所以这两行代码发送了所有这些奇怪的字母组合。这应该发生吗?

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

尝试在您的照片中添加 .encode("utf.8")

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

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

UnicodeEncodeError: 'charmap' codec can't encode characters

python 3.2 UnicodeEncodeError: 'charmap' codec can't encode character '\u2013' in position 9629: character maps to <undefined>