1

I've been trying to learn python these past couple of days and I ran into a problem I'm not quite sure how to solve. I'm trying to make a simple reddit bot and learn the praw reddit API. When I run the following bot:

import praw
import time

r = praw.Reddit('testmachine11968986531')
test = r.submission(id="5u7q8x")

comment_user = set()   # to avoid duplicates

for i in xrange(0,10):  # Run the loop 10 times
    #comments = r.comments(submission)
    for comment in test.comments:
        body = comment.body.lower()
        if body.find("think") != -1 or body.find("please") != -1:
            comment_user.add(comment.author)
    #time.sleep(120)   # Sleep for 2 minutes

print "Here are some comments:"
for user in polite_users:
    print user

I get an error:

RequestException: error with request [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)

I've poked around and saw I can insert something like

verify = False

in a get() instance of sorts, but I'm unsure if that would work in this particular example. Everything else works fine I believe -- I can use pip just fine, etc.

Any help would be appreciated. Thanks a ton.

edit: the full error traceback is

 Traceback (most recent call last):
  File "C:\Users\**\Desktop\Bottest\startBot.py", line 16, in <module>
    for comment in test.comments:
  File "C:\Python27\lib\site-packages\praw\models\reddit\base.py", line 31, in __getattr__
    self._fetch()
  File "C:\Python27\lib\site-packages\praw\models\reddit\submission.py", line 133, in _fetch
    'sort': self.comment_sort})
  File "C:\Python27\lib\site-packages\praw\reddit.py", line 320, in get
    data = self.request('GET', path, params=params)
  File "C:\Python27\lib\site-packages\praw\reddit.py", line 404, in request
    params=params)
  File "C:\Python27\lib\site-packages\prawcore\sessions.py", line 133, in request
    self._authorizer.refresh()
  File "C:\Python27\lib\site-packages\prawcore\auth.py", line 328, in refresh
    password=self._password)
  File "C:\Python27\lib\site-packages\prawcore\auth.py", line 138, in _request_token
    response = self._authenticator._post(url, **data)
  File "C:\Python27\lib\site-packages\prawcore\auth.py", line 29, in _post
    data=sorted(data.items()))
  File "C:\Python27\lib\site-packages\prawcore\requestor.py", line 48, in request
    raise RequestException(exc, args, kwargs)
RequestException: error with request [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:661)
AON
  • 71
  • 3
  • 14
  • What line do you get the error at? – Joseph Chotard Feb 16 '17 at 15:35
  • @claymore-adrendamar I went ahead and edited it in the body – AON Feb 16 '17 at 15:38
  • This might be of help. Not sure though. http://stackoverflow.com/questions/27835619/ssl-certificate-verify-failed-error – Marviel Feb 16 '17 at 15:43
  • Also, have you read the docs on how to use PRAW here: https://praw.readthedocs.io/en/latest/getting_started/quick_start.html ? you need to make sure you have your client_id, client_secret, etc.. (ctrl-f that page for those terms if you're unsure.) I've not used PRAW but this is where I'd start. – Marviel Feb 16 '17 at 15:46
  • 1
    @Marviel Yup, I got those all set in the praw.ini file. I just omitted it due to security and privacy and stuff. – AON Feb 16 '17 at 15:59
  • Possible duplicate of [SSLError with PRAW?](https://stackoverflow.com/questions/21655478/sslerror-with-praw) – Stevoisiak May 01 '18 at 20:43

1 Answers1

0

You problem might reside within your user_agent. Reddit requires a unique user_agent. You user_agent is how you uniquely identify your script. The Reddit API wiki page (https://github.com/reddit/reddit/wiki/API) has the official and updated recommendations on user_agent strings and everything else. Reading it is highly recommended.

Additionaly to this your user_agent string must never contain the string bot. This means you sould change your line:

r = praw.Reddit('bot1')

to something like this:

r = praw.Reddit('Name of your bot [version] by [Your Reddit username or your name]')

[You might want to read this excellent tutorial on the matter https://praw.readthedocs.io/en/v3.6.0/pages/getting_started.html#connecting-to-reddit ]

Otherwise you can add this after creating the praw object:

r.config._ssl_url = None

It is not recommended as it means all your information could be intercepted by a man in the middle

You could also:

add:

r.login('bot_username', 'bot_password')

after r = praw.reddit([...])

change test = r.submission(id="5u7q8x") to test = r.get_submission(submission_id='5u7q8x')

and add this after:

test = praw.helpers.flatten_tree(test.comments)

you should now be able to iterate through test and not test.comments.

If this doesn't work I have no idea what can.

Joseph Chotard
  • 666
  • 5
  • 15
  • I did see a _recommendation_ to not use **bot** in the user agent, but I thought it would only result in problems when the bot would be posting/sending in lieu of just reading and printing back to me. Regardless, I tried changing that and it results in the same error. I updated the body to reflect it – AON Feb 16 '17 at 15:55
  • also, my first simple file: `import praw reddit = praw.Reddit(client_id='xx', client_secret="xx", password='xx', user_agent='testmachine11968986531', username='xx') for submission in reddit.front.hot(): print(submission)` resulted in the same error – AON Feb 16 '17 at 16:01
  • I updated my answer with another *fix* for the problem – Joseph Chotard Feb 16 '17 at 16:04
  • Same thing. `import praw reddit = praw.Reddit('testmachine11968986531') reddit.config._ssl_url = None for submission in reddit.front.hot(): print(submission)` 5 Lines, same error. Could it be something on my end from my workstation? – AON Feb 16 '17 at 16:07