title: How to download file from website that requires login information using Python?
body: <p>I am trying to download some data from a website using Python. If you simply copy and paste the url, it shows nothing unless you fill in the login information. I have the login name and password, however how should I include these in Python?</p>
  
  <p>My current code is:</p>
  
  <pre><code>import urllib, urllib2, cookielib
  
  username = my_user_name
  password = my_pwd
  
  link = 'www.google.com' # just for instance
  cj = cookielib.CookieJar()
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  login_data = urllib.urlencode({'username' : username, 'j_password' : password})
  
  opener.open(link, login_data)
  resp = opener.open(link,login_data)
  print resp.read()
  </code></pre>
  
  <p>There is no error pops out, however resp.read() is a bunch of CSS and it only has the messages like "you have to login before reading news here."</p>
  
  <h2>So how can I retrieve the page that after logging in?</h2>
  
  <p>Just noticed that the website requires 3 entries:</p>
  
  <pre><code>Company: 
  
  Username: 
  
  Password:
  </code></pre>
  
  <p>I have all of them but how can I put all three in the login variable?</p>
  
  <p>If I run it without login it returns:</p>
  
  <pre><code>cj = cookielib.CookieJar()
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  
  opener.open(dd)
  resp = opener.open(dd)
  
  print resp.read()
  </code></pre>
  
  <p>Here is the print-outs:</p>
  
  <pre><code>&lt;DIV id=header&gt;
  &lt;DIV id=strapline&gt;&lt;!-- login_display --&gt;
  &lt;P&gt;&lt;FONT color=#000000&gt;All third party users of this website and/or data produced by the Baltic do so at their own risk. The Baltic owes no duty of care or any other obligation to any party other than the contractual obligations which it owes to its direct contractual partners. &lt;/FONT&gt;&lt;/P&gt;&lt;IMG src="images/top-strap.gif"&gt; &lt;!-- template [strapline]--&gt;&lt;/DIV&gt;&lt;!-- end strapline --&gt;
  &lt;DIV id=memberNav&gt;
  &lt;FORM class=members id=form1 name=form1 action=client_login/client_authorise.asp?action=login method=post onsubmits="return check()"&gt;
  </code></pre>