I am trying to copy files from a website and locally save them, using the following codes. But my local driver shows the saved files are empty (i.e., each excel file is created but contains 1 KB memory). Any comments about why this is not working?
import urllib.request
import pathlib
folder_euronext = "C:/myfolder/"
url_euronext = "https://live.euronext.com/sites/default/files/statistics/factbook/yearly/"
for i in range(2013,2020):
filename = "euronext_fact_book_"+str(i)+".xls" if i <= 2016 else "euronext_fact_book_"+str(i)+".xlsx"
print(url_euronext+filename)
if pathlib.Path(folder_euronext+filename).exists():
print(filename,"exists...")
else:
urllib.request.urlretrieve(url_euronext+filename, folder_euronext+filename)
print("Copying and pasting",filename)
Try this below. You'll need to install requests
import requests
import pathlib
import time
headers = {'User-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36c'}
folder_euronext = pathlib.Path(".")
url_euronext = "https://live.euronext.com/sites/default/files/statistics/factbook/yearly/"
for i in range(2013,2020):
filename = "euronext_fact_book_"+str(i)+".xls" if i <= 2016 else "euronext_fact_book_"+str(i)+".xlsx"
print(url_euronext+filename)
target = folder_euronext / filename
if pathlib.Path(target).exists():
print(filename,"exists...")
else:
resp = requests.get(url_euronext+filename, headers=headers)
with open(target, 'wb') as f:
f.write(resp.content)
print("Copying and pasting",filename)
time.sleep(2.0)