2

I am wondering how I can fill an online form automatically. I have researched it and it tuned out that, one can uses Python ( I am more interested to know how to do it with Python because it is a scripting language I know) but documentation about it is not very good. This is what I found:

Fill form values in a web page via a Python script (not testing)

Even the "mechanize" package itself does not have enough documentation:

http://wwwsearch.sourceforge.net/mechanize/

More specifically, I want to fill the TextArea in this page (Addresses): http://stevemorse.org/jcal/latlonbatch.html?direction=forward

so I don't know what I should look for? Should I look for "id" of the the textArea? ?It doesn't look like that it has "id" (or I am very naive!). How I can "select_form"?

Python, web gurus, please help.

Thanks

Community
  • 1
  • 1
kayhan
  • 373
  • 3
  • 11

1 Answers1

2

See if my answer to the other question you linked helps: https://stackoverflow.com/a/5685569/711017

EDIT: Here is the explicit code for your example. Now, I don't have mechanize installed right now, so I haven't been able to check the code. No online IDE's I checked have it either. But even if it doesn't work, toy around with it, and you should eventually get there:

import re
from mechanize import Browser
br = Browser()
br.open("http://stevemorse.org/jcal/latlonbatch.html?direction=forward")
br.select_form(name="display")
br["locations"] = ["Hollywood and Vine, Hollywood CA"]
response = br.submit()
print response.read()

Explanation: br emulates a browser that opens your url and selects the desired form. It's called display in the website. The textarea to enter the address is called locations, into which I fill in the address, then submit the form. Whatever the server returns is the string response.read(), in which you should find your Lat-Longs somewhere. Install mechanize and check it out.

Community
  • 1
  • 1
Abhranil Das
  • 5,702
  • 6
  • 35
  • 42
  • I checked out your answer. I have not been able to figure it out. I have tried another package called twill which is apparently a wrapper around mechanics. It is slightly easier but I don't know for example how to click a button (is it the same as submit?) – kayhan Dec 23 '11 at 02:33
  • Clicking a button to submit a form is the same as the submit, yes. – Abhranil Das Dec 23 '11 at 07:27