title: python urllib2 document.login
body: <p>How would you go about logging into a website that is set up like so, using python urllib2</p>
  
  <p>The below is the javascript handler on the form and a onsubmit. How would I process this in python?</p>
  
  <pre><code>&lt;script&gt;
  
  function handleLogin() {document.login.un.value = document.login.username.value;document.login.width.value = screen.width;document.login.height.value = screen.height;}
  
  &lt;/script&gt;
  </code></pre>
  
  <p>Below is the html form with all the components to send as post. What holds me up is the onsubmit function. </p>
  
  <pre><code>&lt;form id='login' name='login' method='post' onsubmit="handleLogin();" action='login.php'&gt;
  
  &lt;input class="txtbox glow" type="text" id="username" name="username" value="blanksss"&gt;
  
  &lt;input class="txtbox glow" type="password" id="password" name="pw" size="18" autocomplete="off" onkeypress="checkCaps(event)"&gt;
  
  &lt;input class="checkbox" type="checkbox" id="rememberUn" name="rememberUn" checked="checked"&gt;
  
  &lt;input class="loginButton" type="submit" id="Login" name="Login" value="Login"&gt;
  
  &lt;/form&gt;
  </code></pre>
  
  <p>Python Code I have that works with easy sites without javascript is:</p>
  
  <pre><code>cj = cookielib.CookieJar()
  opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  opener.addheaders.append(('User-agent', 'Mozilla/4.0'))
  opener.addheaders.append( ('Referer', 'https://login.site.com/') )
  login_data = urllib.urlencode({'username' : 'mylogin',
                                 'pw' : 'mypass',
                                 'Login' : 'Login',
                                 'rememberUn' : 'checked'
                                 })
  resp = opener.open('https://login.site.com/', login_data)
  website = opener.open('https://eu1.site.com/a03/o')
  
  bowl = BeautifulSoup(website)
  </code></pre>
  
  <p>Any thoughts on how to process javascript code then post the finished results to the action of the form?</p>
  
  <p>Thanks</p>