Form Post Trouble Using urllib

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Allgood

    Form Post Trouble Using urllib

    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.urlencod e(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
  • bromden

    #2
    Re: Form Post Trouble Using urllib

    first, the url you should request is

    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
    [color=blue]
    > url = "http://www.usps.com/zip4/zip4_response.j sp"[/color]
    ....[color=blue]
    > 'address': '1600 pennsylvania',
    > 'address1': '',
    > 'address2': '',[/color]
    ....

    --
    bromden[at]gazeta.pl

    Comment

    Working...