title: can't download cookie protected file with python
body: <p>All day long I'm looking to solve this. 
  There is this <a href="http://www.some.site/index.php" rel="nofollow">http://www.some.site/index.php</a> that is requesting user and password + sends cookie. Alright, I get in like this: </p>
  
  <pre><code>import urllib, urllib2, cookielib, os
  import re # not required here but tried it out though
  import requests # not required here but tried it out though
  username = 'somebody'
  password = 'somepass'
  
  cj = cookielib.CookieJar()
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  login_data = urllib.urlencode({'username' : username, 'j_password' : password})
  resp = opener.open('http://www.some.site/index.php', login_data)
  print resp.read()
  </code></pre>
  
  <p>The problem is that in the middle of the screen there is a link to download an .xls file: <a href="http://www.some.site/excel_file.php?/t=1303457489" rel="nofollow">http://www.some.site/excel_file.php?/t=1303457489</a>. I can download the file in any Browser (Mozilla, Chrome, IE) but not with Python. After the .php the <strong>post data</strong> ( ie: ?t=1370919996 ) is changing all the time when I login or Refresh the page. </p>
  
  <p>Maybe I'm wrong but I believe the Post Data is generated from the cookie (or session-cookie), but the cookie contains only this: <code>('set-cookie', 'PHPSESSID=9cde55534fcc8e136fcf6588c0d0f1df; path=/')</code> </p>
  
  <p>This is one way I tried to save the file:</p>
  
  <pre><code>print "downloading with urllib2"
  f = urllib2.urlopen('http://www.some.site/excel_file.php')
  data = f.read()
  with open("exceldoc.xls", "wb") as code:
      code.write(data)
  </code></pre>
  
  <p>if I save it or if I print it out gives the same bad request error:</p>
  
  <pre><code>&lt;b&gt;Fatal error&lt;/b&gt;:  Call to a member function FetchRow() on a non-object in &lt;b&gt;http://www.some.site/excel_file.php&lt;/b&gt; on line &lt;b&gt;112&lt;/b&gt;&lt;br 
  </code></pre>
  
  <p><strong>How can I download this file with Python?</strong> Thank you so much in advance for any help!</p>
  
  <p>There are many similar posts, I've checked them and my examples are inspired from those yet nothing worked for me. I'm not very familiar with <a href="http://en.wikipedia.org/wiki/HTTP_cookie" rel="nofollow">cookies</a>, php, js.</p>
  
  <p>EDIT: this is what I get when I print out the content of index.php: </p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;SOMETITLE&lt;/title&gt;
  &lt;meta http-equiv="Page-Enter" content="blendTrans(Duration=0.5)"&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
  &lt;link rel='stylesheet' type='text/css' href='somesite.css'&gt;
  &lt;SCRIPT LANGUAGE="JavaScript"&gt;
  &lt;!-- JavaScript hiding
  
  function clearDefault(obj) {
      if (!obj._cleared) {
                  obj.value='';
                  obj._cleared=true;
      }
  }
  
  // --&gt;
  &lt;/SCRIPT&gt;
  &lt;/head&gt;
  
  &lt;body bgcolor="#FFFFFF" text="#000000"&gt;
  
  &lt;table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;table width="1000" height="150" border="0" align="center" cellpadding="16" cellspacing="0" class="header" style="background: #989896 url('images/header.png') no-repeat;"&gt;
          &lt;tr&gt;
            &lt;td valign="middle"&gt;
              &lt;table width="100%" border="0" align="center" cellpadding="0" cellspacing="0"&gt;
                &lt;tr&gt;
                  &lt;td width="380"&gt;&amp;nbsp;&lt;/td&gt;
                  &lt;td&gt;
                    &lt;div id="login"&gt;
                         &lt;form name="flogin" method="post" action="/index.php"&gt;
                        &lt;h1&gt;Login&lt;/h1&gt;
                        &lt;input name="uName" type="text" value="Username:" class="name" onfocus="clearDefault(this)"&gt;
                        &lt;br&gt;
                        &lt;input type="password" name="uPw"  value="Password:" class="pass" onfocus="clearDefault(this)"&gt;
                        &lt;input type="submit" name="Submit" value="OK" class="submit"&gt;
                      &lt;/form&gt;
                    &lt;/div&gt;                                                                
                                                                  &lt;/td&gt;
                &lt;/tr&gt;
              &lt;/table&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/table&gt;
                  &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>