pythonscriptingform-submitdata-harvest

Automatically pressing a "submit" button using python


The bus company I use runs an awful website (Hebrew,English) which making a simple "From A to B timetable today" query a nightmare. I suspect they are trying to encourage the usage of the costly SMS query system.

I'm trying to harvest the entire timetable from the site, by submitting the query for every possible point to every possible point, which would sum to about 10k queries. The query result appears in a popup window. I'm quite new to web programming, but familiar with the basic aspects of python.

  1. What's the most elegant way to parse the page, select a value fro a drop-down menu, and press "submit" using a script?
  2. How do I give the program the contents of the new pop-up as input?

Thanks!


Solution

  • Twill is a simple scripting language for Web browsing. It happens to sport a python api.

    twill is essentially a thin shell around the mechanize package. All twill commands are implemented in the commands.py file, and pyparsing does the work of parsing the input and converting it into Python commands (see parse.py). Interactive shell work and readline support is implemented via the cmd module (from the standard Python library).

    An example of "pressing" submit from the above linked doc:

    from twill.commands import go, showforms, formclear, fv, submit
    
    go('http://issola.caltech.edu/~t/qwsgi/qwsgi-demo.cgi/')
    go('./widgets')
    showforms()
    
    formclear('1')
    fv("1", "name", "test")
    fv("1", "password", "testpass")
    fv("1", "confirm", "yes")
    showforms()
    
    submit('0')