为什么我的Reddit机器人与PRAW返回意外的字母串?

问题描述 投票:0回答:1

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

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

这个机器人大部分都能用。这是代码。

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

我删掉了所有的个人信息 比如密码和名字 I censored all the personal information such as passwords and names. 现阶段的代码应该是返回机器人的名字 "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

我不知道这些字母应该代表什么 I have no idea what these letters are supposed to represent. 我试着把代码中的特定部分注释出来,看看是否能确定是什么原因造成的。我让它产生了类似的结果,只有第2行和第10行。所以,这两行代码的某些地方发送了所有这些奇怪的字母组合。这是应该发生的吗?

python botframework reddit
1个回答
0
投票

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

试着加上 .encode("utf.8") 在你的打印中。

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

如果不行的话,你也可以尝试在你收到错误的地方加入编码="utf-8 "作为参数(我看不出是哪里)

UnicodeEncodeError: 'charmap' 编解码器不能编码字符。

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

© www.soinside.com 2019 - 2024. All rights reserved.