Programmatically login to OKC
Question: I'm trying to programatically login to OKCupid
([www.okcupid.com/login](http://www.okcupid.com/login))in order to fetch
scrape some user data. I've tried to put together a python script to do this,
but seem to be doing something wrong.

The behavior I'd like from this sample script is to login, follow a redirect
to the homepage then print the HTML response. Here's what I have so far:

    
    
    import urllib, urllib2, cookielib
    
    # cookie storage
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(
        urllib2.HTTPCookieProcessor(cj),
        urllib2.HTTPRedirectHandler
        )
    # Useragent
    opener.addheaders.append(('User-agent','Mozilla/4.0'))
    
    url = 'http://www.okcupid.com/login'
    login_data = urllib.urlencode({
        'username':'myusername',
        'password':'mypassword',
        })
    
    req = urllib2.Request(url,login_data)
    resp = urllib2.urlopen(req)
    the_page = resp.read()
    
    print the_page
    


Answer: based on what you are doing, here's an example of what i'm doing to login,
then go to messages and save all usernames

    
    
    import urllib, urllib2, cookielib
    import re
    # cookie storage
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(
        urllib2.HTTPCookieProcessor(cj),
        urllib2.HTTPRedirectHandler
        )
    # Useragent
    opener.addheaders.append(('User-agent','Mozilla/4.0'))
    
    
    url = 'http://www.okcupid.com/login'
    login_data = urllib.urlencode({
        'username':'Mobius1',
        'password':'raptor22',
        })
    
    urllib2.install_opener(opener)
    
    res = opener.open(url, login_data)
    
    print res.url #should be http://www.okcupid.com/home if successful
    res.close()
    
    #navigate profile after successful login
    res = opener.open('http://www.okcupid.com/messages')
    the_page = res.read() #read content at URL
    
    #find all usernames from page content with pattern /profile/username?
    UserNameList = re.findall(r'/profile/([\w\.-]+)?', the_page)
    print UserNameList
    
    with open('OkC_messages.html', 'w') as fid: #save the_page as html
        fid.write(the_page)
    

