Skip to content

Instantly share code, notes, and snippets.

@agrouaze
Created April 25, 2024 15:14
Show Gist options
  • Save agrouaze/752467841cb82b13cbbb7dbb51cdae64 to your computer and use it in GitHub Desktop.
Save agrouaze/752467841cb82b13cbbb7dbb51cdae64 to your computer and use it in GitHub Desktop.
example of python script to download the files.
import os
import requests
import logging
def list_and_download_files(base_url, directory, download_dir):
# Make a request to the directory URL
url = base_url + directory
response = requests.get(url)
logging.info('response: %s',response)
if response.status_code == 200:
# Create download directory if it doesn't exist
if not os.path.exists(download_dir):
os.makedirs(download_dir)
# Parse HTML content to find links to files
file_links = []
for line in response.text.split('\n'):
if 'href="' in line:
start_index = line.find('href="') + len('href="')
end_index = line.find('"', start_index)
link = line[start_index:end_index]
file_links.append(link)
# Download nc files one by one
for file_link in file_links:
if '.nc' in file_link:
file_url = base_url + directory + file_link
logging.info('file_link : %s',file_link)
filename = os.path.join(download_dir, os.path.basename(file_link))
print("Downloading:", filename)
with open(filename, 'wb') as f:
file_response = requests.get(file_url)
f.write(file_response.content)
print("Downloaded:", filename)
else:
print("Failed to retrieve directory listing.")
# Example usage
logging.basicConfig(level=logging.INFO)
base_url = "https://cerweb.ifremer.fr/"
download_dir = '/tmp/test_wget/' # change it
directory = "/datarmor/sarwave/diffusion/sar/iw/slc/l2/cross-comparison_product_collection/17.5km/S1A_IW_XSP__1SDV_20200214T021047_20200214T021114_031242_0397D1_3E99.SAFE"
list_and_download_files(base_url, directory,download_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment