[Tutor] Webform

Kent Johnson kent37 at tds.net
Tue Mar 27 14:04:55 CEST 2007


Øyvind wrote:
> I am trying to fill in a webform, and have tried using
> Clientform/mechanize, but cannot seem to get it right.
> 
> It just gives me feedback that the form is processed, but I can't seem to
> get the result I get if I manually enter the value with the browser.

This form is doing some funny stuff with JavaScript. Take a look at the 
page source. The button you click is not the actual submit button, 
clicking it calls _doClick() with a long parameter. This parameter is 
set as the value of the hidden __Click field before submitting the form.

I don't know for sure why this is done, but probably to thwart machine 
harvesting of the data such as you are doing. You should make sure you 
aren't violating the terms of use of the website by automatically 
fetching the data.

Anyway this seems to work:
In [1]: import urllib2
In [2]: from ClientForm import ParseResponse
In [4]: response = 
urllib2.urlopen("http://www.datateam.no/boc/bocadresse.nsf/wMedlemsoek?OpenForm&Seq=1")
In [5]: forms = ParseResponse(response, backwards_compat=False)
In [6]: form = forms[0]
In [10]: click = form.find_control('__Click')
In [11]: click.readonly=False
In [12]: form['__Click'] = 
'41256C7C00368A2B.8b5a45b319030f3ec125728a00788ddb/$Body/0.CB4'
In [13]: form["QueryField"] = """980213250"""
In [20]: print urllib2.urlopen(form.click()).read()

The value for the __Click field doesn't seem to change, but if it does 
you will have to parse it out of the onclick atttribute of the SubmitBtn 
control.

Kent


More information about the Tutor mailing list