最近用到了GLASS的LAI產品,但這個產品的文件夾分得很細,我需要的影像又有8個瓦片,一個一個點擊很麻煩,于是探索了批量下載的方法
一、下載1幅
import requests
import re
import os
import requests
import re# 網頁URLurl = "http://www.glass.umd.edu/LAI/MODIS/250m/2003/009/" #網址# 本地文件夾路徑
local_folder = r"F:\BH\1.data\2.area data\LAI"# 確保本地文件夾存在
if not os.path.exists(local_folder):os.makedirs(local_folder)# 發送HTTP請求獲取網頁內容
response = requests.get(url)# 使用正則表達式提取文件鏈接
file_links = re.findall(r'href=[\'"]?([^\'" >]+)', response.text)# 遍歷文件鏈接,下載包含特定關鍵詞的文件 hdf
for file_link in file_links:if ".hdf" in file_link and "h26v05" in file_link:# 下載文件file_url = url + "/" + file_linkfile_name = os.path.join(local_folder, file_link.split("/")[-1])with open(file_name, 'wb') as f:response_file = requests.get(file_url)f.write(response_file.content)print("ok")
二、下載n
import requests
import re
import os
import requests
import re# 網頁URL
years=['2003','2004','2005','2006','2007','2008','2009','2010','2011','2012','2013','2014','2015','2016','2017','2018','2019','2020','2021']
days=['001','009','017','025','033','041','049','057','065','073','081','089','097','105','113','121','129','137','145','153','161','169','177','185','193','201','209','217','225','233','241','249','257','265','273','281','289','297','305','313','321','329','337','345','353','361']for i in range(0,len(years)):for j in range(0,len(days)):url = "http://www.glass.umd.edu/LAI/MODIS/250m/"+years[i]+"/"+days[j]+"/" #網址# 本地文件夾路徑local_folder = r"F:\BH\1.data\2.area data\LAI"# 確保本地文件夾存在if not os.path.exists(local_folder):os.makedirs(local_folder)# 發送HTTP請求獲取網頁內容response = requests.get(url)# 使用正則表達式提取文件鏈接file_links = re.findall(r'href=[\'"]?([^\'" >]+)', response.text)# 遍歷文件鏈接,下載包含特定關鍵詞的文件 hdffor file_link in file_links:if ".hdf" in file_link and ("h24v05" in file_link or "h25v05" in file_link or "h26v05" in file_link or "h27v05" in file_link or "h24v06" in file_link or "h25v06" in file_link or "h26v06" in file_link or "h27v06" in file_link or "h28v06" in file_link ):# 下載文件file_url = url + "/" + file_linkfile_name = os.path.join(local_folder, file_link.split("/")[-1])with open(file_name, 'wb') as f:response_file = requests.get(file_url)f.write(response_file.content)print("ok")