最近用到了GLASS的LAI产品,但这个产品的文件夹分得很细,我需要的影像又有8个瓦片,一个一个点击很麻烦,于是探索了批量下载的方法
一、下载1幅
import requests
import re
import os
import requests
import re
# 网页URL
url = "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_link
file_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)
# 遍历文件链接,下载包含特定关键词的文件 hdf
for 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_link
file_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")