Jesús León
Jesús León

Reputation: 157

Log in to a website with Python

I've been trying to log into http://www.qualtrics.com/login/ and then save a cookies file but it won't work.

opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
opener.addheaders =[('Referer', 'http://www.qualtrics.com'),
('User-Agent','Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0'),
                        ('Content-Type','application/x-www-form-urlencoded')]
url = 'http://www.qualtrics.com/login/'
data = {'method' : '1', 'login' : 'my-username', 'password' : 'my-password'}
req = urllib2.Request(url, urllib.urlencode(data))
res = opener.open(req)

But the response is what someone without account would see (it's not working). Any help? Also, the cookies file should look like this:

# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
value / value / value etc

Upvotes: 4

Views: 215

Answers (2)

Slick
Slick

Reputation: 359

While this seems a little broad wouldn't selenium provide you with a good base to start from. There are a lot of good examples to pull from. Stealing from this example

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://www.example.com')
browser.find_element_by_name('username').send_keys('myusername')
browser.find_element_by_name('password').send_keys('mypassword')
browser.find_element_by_class_name('welcomeLoginButton').click()
cookies = browser.get_cookies()

You may need to put some wait statements in there but there are lots of examples here on Stack Overflow and lots of info in the docs

EDIT: Fixed link to the documentation.

  • The find_element_by_name can be found here.
  • More info on cookies can be found here. For this example the cookies object is just a dict of { name:value }

Upvotes: 4

Bo Milanovich
Bo Milanovich

Reputation: 8203

There is an interesting library called mechanize. It is not very up-to-date, but it works fine.

You'd have something like this:

import mechanize

browser = mechanize.Browser()
browser.open("http://www.example.com")
browser.select_form(name="myform")
browser["username"] = "username"
browser["password"] = "password"
browser.submit()

It does all the cookie automation for you (although you can override that behavior). You'll also likely need to add headers (referrer, user-agent, etc.).

The library is available here: http://wwwsearch.sourceforge.net/mechanize/

EDIT: Their (admittedly poor) documentation also explains how you can save/manipulate cookies.

Upvotes: 2

Related Questions