Search code examples
pythonurllib2urllib

Python Urllib POST response is not fetching correct values


For learning POST parmaeter urllib, I'm trying to grab table values for a particular date in the following code entered in the parameters. However, it doesn't return the values for 12th September but instead response shows the date is 12th October.

Using POSTMAN, the response is returned for the correct date but with Python, I'm unable to obtain values for other than current month. Any explanation to what could be causing this? any help/suggestion is appreciated.

import urllib
import urllib2


url = ''

data = urllib.urlencode({'priceDate.month' : '09', 'priceDate.date' : '12','priceDate.year':'2016','submit':'Show Prices'})
req = urllib2.Request(url, data)

response = urllib2.urlopen(req)

d = response.read()

print d

Solution

  • Just use requests module.

    import requests as re
    url = "https://www.treasurydirect.gov/GA-FI/FedInvest/selectSecurityPriceDate.htm"
    parms = {'priceDate.month':'09','priceDate.day':'12','priceDate.year':'2016','submit':'CSV+Format'}
    resp = re.post(url, parms)
    resp.content