Your code is almost perfect. Very good!
I decided to test your code with the following dates:
startdate = '20170130'
enddate = '20170207'
When testing with a shorter period, I could see the following:
- This error only appears when there were no trades on the day (Sat, Sun or Holidays), ie the file does not really exist!
- You can use a try / except structure to work around this error
Example Code:
from io import BytesIO
from urllib.request import urlopen
from zipfile import ZipFile
import pandas as pd
startdate = '20170130'
enddate = '20170207'
extension = '.zip'
daterange = pd.date_range(start=startdate, end=enddate)
for single_date in daterange:
zipurl = 'ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_' + single_date.strftime('%Y%m%d') + extension
try:
with urlopen(zipurl) as zipresp:
with ZipFile(BytesIO(zipresp.read())) as zfile:
zfile.extractall()
print("OK: {}".format(zipurl))
except:
print('ERRO: {}'.format(zipurl))
print("="*5)
Output:
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170130.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170131.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170201.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170202.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170203.zip
=====
ERRO: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170204.zip
=====
ERRO: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170205.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170206.zip
=====
OK: ftp://ftp.bmf.com.br/MarketData/BMF/NEG_BMF_20170207.zip
=====
Checking in the calendar, we can see that 04/02/2017
was a sábado
and 05/02/2017
a domingo
.
And when entering the folder with the supposed downloads, surprise:
Note that I removed the extraction output path for testing purposes. You can reinstall it in your code (% with%)