Form Post Trouble Using urllib

Discussion in 'Python' started by Steve Allgood, Oct 15, 2003.

  1. 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
    Steve Allgood, Oct 15, 2003
    #1
    1. Advertising

  2. Steve Allgood

    bromden Guest

    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': '',

    ....

    --
    bromden[at]gazeta.pl
    bromden, Oct 15, 2003
    #2
    1. Advertising

  3. Thank you Bromden. That worked! I didn't think of checking the Java.

    ~Steve
    Steve Allgood, Oct 16, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russell E. Owen

    trouble with urllib and ftps

    Russell E. Owen, Nov 17, 2004, in forum: Python
    Replies:
    0
    Views:
    336
    Russell E. Owen
    Nov 17, 2004
  2. Pieter Edelman
    Replies:
    0
    Views:
    790
    Pieter Edelman
    Mar 25, 2005
  3. Jonathan Gardner

    Asynchronous urllib (urllib+asyncore)?

    Jonathan Gardner, Feb 26, 2008, in forum: Python
    Replies:
    1
    Views:
    462
    Terry Jones
    Feb 27, 2008
  4. Adam W.
    Replies:
    1
    Views:
    609
    Jon Clements
    Sep 24, 2009
  5. Chris McDonald
    Replies:
    0
    Views:
    275
    Chris McDonald
    Nov 1, 2010
Loading...

Share This Page