automatic form filling

harold fellermann harold.fellermann at upf.edu
Tue Jul 12 12:17:19 EDT 2005


> I would like to know how I could automatically fill a
> (search) form on a web page and download the resulting
> html page. More precisely I would like to make a
> program that would automatically fill the "Buscador
> lista 40" (in spanish, sorry) form in the following
> webpage:
> http://www.los40.com/actualidad/listas/lista40.html
> and download the results for several dates
> (dia/mes/año = day/month/year).
> I am not sure this is the right place to ask, but I
> would be very grateful if anybody can help or redirect
> me to another mailing list...


The relevant part of the sites source code is the following:

<FORM method="POST" action="busquedas_b.html" name="porFecha">
<TR>
	<TD BGCOLOR="#333333" WIDTH="2"><img SRC="/images/0.gif" WIDTH="4" 
HEIGHT="4" BORDER="0"></TD>
	<TD align="right" WIDTH="25" BGCOLOR="#666666"><font face="Arial, 
Helvetica, sans-serif" size="1" color="#666666"><input type="text" 
name="byDay"  class="campotxt9" size="1" value="dia" 
onFocus="if(this.value=='dia')this.value='';" 
onblur="if(this.value=='')this.value='dia';"></TD>
	<TD align="right" WIDTH="15"  BGCOLOR="#666666"><font face="Arial, 
Helvetica, sans-serif" size="1" color="#666666"><input type="text" 
name="byMonth" class="campotxt9" size="1" value="mes" 
onFocus="if(this.value=='mes')this.value='';" 
onblur="if(this.value=='')this.value='mes';"></TD>
	<TD align="right" WIDTH="25"  BGCOLOR="#666666"><font face="Arial, 
Helvetica, sans-serif" size="1" color="#666666"><input type="text" 
name="byYear" class="campotxt2" size="1" value="año" 
onFocus="if(this.value=='año')this.value='';" 
onblur="if(this.value=='')this.value='añ';"></TD>
	<TD BGCOLOR="#333333" align="left"><A 
href="javascript:document.porFecha.submit();"><img 
SRC="/images/ir4.gif" BORDER="0" title="Ir"></a></TD>
</TR>
</FORM>


from the first line you can see that submitting the result will request 
the
page "busquedas_b.html" -- so your script has to request this site 
directly
with the form parameters given as additional data (method="POST"). which
form parameters to pass can be seen in the name attributes of the 
input-tags,
e.g. <input type="text" name="byYear">.

urllib provides the methods urlopen and urlencode to setup the query 
string
and fetch the result:

from urllib import urlopen,urlencode

form_data = {
	'byDay' : '12' ,
	'byMonth' : '07' ,
	'byYear' : '2005'
}
html_data = urlopen(
	"http://www.los40.com/actualidad/listas/busquedas_b.html",
	urlencode(form_data)
).read()

print html_data


- harold -

--
"I know what I believe. I will continue to articulate what I believe
  and what I believe - I believe what I believe is right."
-- George W. Bushman




More information about the Python-list mailing list