Attribute error while retrieving data from url with zip file

I am trying to access the "yield curve data" available on this page . He has a switch, which after clicking "Submit" leads to the zip file from which I am looking to get the data. I am looking to get data from the "Get all data" option. My code is as follows, however, I get an error AttributeError: readin the line zipfile = ZipFile(StringIO(request.read())), and I do not know how to solve this problem.

import urllib, urllib2
import csv
from StringIO import StringIO
import pandas as pd
import os
from zipfile import ZipFile

my_url = 'http://www.bankofcanada.ca/rates/interest-rates/bond-yield-curves/'
data = urllib.urlencode({'lastchange': 'all'}) 
request = urllib2.Request(my_url, data)
result = urllib2.urlopen(request)

zipfile = ZipFile(StringIO(request.read()))

thank

+4
source share
2 answers

After decrypting the html source of the linked web page , it would seem to work - at least as far as I understood:

import urllib, urllib2
import csv
from StringIO import StringIO
import pandas as pd
import os
from zipfile import ZipFile
from pprint import pprint, pformat

my_url = 'http://www.bankofcanada.ca/stats/results/csv'
data = urllib.urlencode({"lookupPage": "lookup_yield_curve.php",
                         "startRange": "1986-01-01",
                         "searchRange": "all"})
request = urllib2.Request(my_url, data)
result = urllib2.urlopen(request)
zipdata = result.read()
zipfile = ZipFile(StringIO(zipdata))
print 'zipfile.namelist(): {}'.format(zipfile.namelist())

Conclusion:

zipfile.namelist(): ['yield_curves.csv']
+2

urllib2 urllib2.Request read. - result.read(), -

zipfile = ZipFile(StringIO(result.read()))
0

All Articles