using cgi form via http and extracting results

Joe Francia usenet at -OBFUSCATION-joefrancia.com
Tue Jan 6 14:52:14 EST 2004


bmgx wrote:
> Ok it's done right..
> 
> import urllib
> a = urllib.urlopen("http://www.suckerservice.com/convert.cgi", 
> "Amount=1&From=EUR&To=USD")
> b = a.readlines()
> c = open("tempresult.html","w")
> 
> i = 0
> d = "********* local copy ******\n\n"
> while i < len(b):
>     d = d + b[i]
>     i = i + 1
> 
> c.write(d)
> c.close()
> a.close()
> 

Instead of constructing the data string by hand, you can pass a 
dictionary or list of 2-item tuples to urllib.urlencode() and get a 
properly formatted string:

my_data = urllib.urlencode({'Amount':1,'From':'EUR','To':'USD'}) # or...
my_data = urllib.urlencode([('Amount',1),('From','EUR'),('To','USD')])
a = urllib.urlopen('http://www.suckerservice.com/convert.cgi', my_data)



More information about the Python-list mailing list