Making HTTP POST request

前端 未结 3 1518
失恋的感觉
失恋的感觉 2020-12-23 12:10

I\'m trying to make a POST request to retrieve information about a book. Here is the code that returns HTTP code: 302, Moved

import httplib, urllib
params =          


        
相关标签:
3条回答
  • 2020-12-23 12:44

    Their server seems to want you to acquire the proper cookie. This works:

    import urllib, urllib2, cookielib
    
    cookie_jar = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
    urllib2.install_opener(opener)
    
    # acquire cookie
    url_1 = 'http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackMaterialsView?langId=-1&catalogId=10001&storeId=10051&schoolStoreId=15828'
    req = urllib2.Request(url_1)
    rsp = urllib2.urlopen(req)
    
    # do POST
    url_2 = 'http://www.bkstr.com/webapp/wcs/stores/servlet/BuybackSearch'
    values = dict(isbn='9780131185838', schoolStoreId='15828', catalogId='10001')
    data = urllib.urlencode(values)
    req = urllib2.Request(url_2, data)
    rsp = urllib2.urlopen(req)
    content = rsp.read()
    
    # print result
    import re
    pat = re.compile('Title:.*')
    print pat.search(content).group()
    
    # OUTPUT: Title:&nbsp;&nbsp;Statics & Strength of Materials for Arch (w/CD)<br />
    
    0 讨论(0)
  • 2020-12-23 13:00
    1. Perhaps that's what the browser gets, and you'll just have to follow the 302 redirect.

    2. If all else fails, you can monitor the dialogue between Firefox and the Web Server using FireBug or tcpdump or wireshark, and see which HTTP headers are different. Possibly it's just the User Agent: header.

    0 讨论(0)
  • 2020-12-23 13:08

    You might want to use the urllib2 module which should handle redirects better. Here's an example of POSTING with urllib2.

    0 讨论(0)
提交回复
热议问题