Search code examples
pythonreddit

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


I am working on a Reddit bot whose goal is to determine how often Trump is mentioned in popular political subreddits.

I've been following a few tutorials, I'll link them here just for clarity's sake:

The bot works for the most part. Here's the code:

#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. The code at this stage is supposed to return the name of the bot, "TrumpSpamBot", and then head into a specific Reddit post, and start listing all the top level comments. Currently, it lists one, kind of, and then spits out a bunch of seemingly random combination of letters. Results can be seen here:

[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. I tried commenting out specific parts of the code to see if I could identify what was causing it. I had it producing a similar result with only lines 2 and 10. So something with these two lines of code is sending all these weird combinations of letters. Is this supposed to happen?


Solution

  • I'm not very familiarized with this kind of error but I think it's a problem with the encoding. The standard is utf-8, I did a bit of research:

    Try adding .encode("utf.8") in your prints:

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

    If it doesn't work you can also try adding encoding="utf-8" as a parameter where you're getting the error(I can't really see where it is)

    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>