Form Post Trouble Using urllib

S

Steve Allgood

I'm having trouble posting a form at the USPS web site. I've been
successful using urllib at other sites, but I'm missing why this won't
work:

# begin code
# get zip+4

import urllib

def zip4query():
url = "http://www.usps.com/zip4"
data = {
'Selection': '1',
'urbanization': '',
'firm': '',
'address': '',
'address1': '1600 pennsylvania',
'address2': '',
'city': 'washington',
'state': 'DC',
'zipcode': '',
}
urldata = urllib.urlencode(data)
results = urllib.urlopen(url, urldata).read()
print results

zip4query()

# end code

I just get the query form back as my results. I didn't have this
problem before. What am I doing wrong?

Thanks,
Steve
 
B

bromden

first, the url you should request is
http://www.usps.com/zip4/zip4_response.jsp
it is the "action" attribute of the form submitted as you can see when
you view source of http://www.usps.com/zip4,

then, when you trace javascript executed on the form submission,
you'll notice that the field "address" is filled with the result of
concatenation of "address1" and " " and "address2"

update these two bits and it'll work as you intended
> url = "http://www.usps.com/zip4/zip4_response.jsp" ....
> 'address': '1600 pennsylvania',
> 'address1': '',
> 'address2': '',
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top