Search code examples
pythonvbulletin

Logging into vBulletin 4.0 via python


I am trying to login to a vBulletin 4.0 site using python. This is my code:

import cookielib
import urllib2
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login_data = urllib.urlencode({'username' : 'user', 'j_password: 'password'})
opener.open('http://www.example.com/forum/login.php?do=login', login_data)

I don't understand how to receive the necessary stuff back to know whether I logged in or not though, how would I do that?

Thanks.


Solution

  • I would strongly suggest using python requests module.

    With it you can forget about the the nuance and annoyances of urllib.

    So your code would reduce to something along the lines of:

    import requests
    
    params = {'username' : 'user', 'j_password: 'password'}
    
    r = requests.get('http://www.example.com/forum/login.php?do=login', params=params)
    
    r.raise_for_status()