0

I am trying to login to reddit using python(not PRAW). Below is the code

import mechanize
import cookielib
import urllib
import logging
import sys

br = mechanize.Browser()
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

br.set_handle_equiv(True)
br.addheaders=[('user-agent','Mozilla-Firefox')]
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

r= br.open('http://www.reddit.com')

    # Select the second (index one) form
br.select_form(nr=1)

    # User credentials
br.form['user'] = 'user'
br.form['passwd'] = 'passwd'

    # Login
x=br.submit()

this answer claims the code to be working. but when I print x.geturl(). It gives

'https://www.reddit.com/post/login'

So my question is, isn't this code be enough to login to reddit. or am I missing something. And also how do I verify, whether or not I have logged in?

Thanks

Community
  • 1
  • 1
paramvir
  • 276
  • 4
  • 22

1 Answers1

0

So I found the solution this problem. turns out that python was inputing my password wrong. It was like "******\***". So as it is clear, it has backslash in it. But in python backslash is an escape character. So in order to fix this, I did this "******\\***". Notice the double backslash. Otherwise this method is flawless and it does work.

Hope it helps somebody someday.

paramvir
  • 276
  • 4
  • 22