标记
GOCI-II数据集下载脚本
数据集网站:
https://kosc.kiost.ac.kr/gociSearch/list.nm?menuCd=11&lang=ko&url=gociSearch&dirString=/COMS/GOCI/L2/
有批量下载数据集的需求,直接python运行脚本
如果想下载slot的部分数据,可以按照上一篇文章下载:
选择部分slot下载数据集:https://blog.csdn.net/qq_44224801/article/details/143882622
import urllib.request
import datetime
import os
# 下载文件函数
def download_file(url, output_dir):
"""
下载文件到指定目录
"""
try:
# 获取文件名
file_name = url.split("/")[-1].split("&")[0] # 提取文件名
output_path = os.path.join(output_dir, file_name)
print(f"正在下载: {file_name}")
urllib.request.urlretrieve(url, output_path)
print(f"文件已保存: {output_path}")
except Exception as e:
print(f"下载失败 {url},错误:{e}")
# 检测数据函数
def check_all_files_dynamic(year, month, day, target_suffix=".TSS.he5.zip"):
"""
检查指定日期是否有目标数据,并返回文件的链接
"""
url = f'http://kosc.kiost.ac.kr/gociSearch/list.nm?menuCd=11&lang=ko&url=gociSearch&dirString=/COMS/GOCI/L2/{year}/{month}/{day}/L2'
try:
# 请求URL并解析HTML
response = urllib.request.urlopen(url)
html = response.read().decode('utf-8')
except Exception as e:
print(f"无法访问 {url},错误:{e}")
return []
all_files = []
num = 0 # 起始索引
while True:
try:
# 提取文件路径
index0 = html.index(f'realPath" name="downloadVOList[{num}].realPath" value="') + len(f'realPath" name="downloadVOList[{num}].realPath" value="')
index1 = html.index('"', index0)
filename = html[index0:index1]
# 检查文件路径是否符合目标后缀
if filename.endswith(target_suffix):
file_url = f"http://kosc.kiost.ac.kr/{filename}"
all_files.append(file_url)
num += 1 # 检查下一个文件
except ValueError:
# 如果找不到文件索引,说明没有更多文件
break
except Exception as e:
print(f"解析文件 {num} 数据时出错:{e}")
break
return all_files
# 遍历日期范围并下载
def monitor_and_download_files(start_date, end_date, output_dir, target_suffix=".TSS.he5.zip"):
"""
遍历指定日期范围,监测并下载目标后缀文件
"""
current_date = start_date
while current_date <= end_date:
year = current_date.year
month = f"{current_date.month:02d}"
day = f"{current_date.day:02d}"
print(f"正在检查日期: {year}-{month}-{day}...")
all_files = check_all_files_dynamic(year, month, day, target_suffix=target_suffix)
if all_files:
print(f"日期 {year}-{month}-{day} 检测到 {len(all_files)} 个有效文件:")
for file_url in all_files:
print(f" 文件链接: {file_url}")
download_file(file_url, output_dir) # 下载文件
else:
print(f"日期 {year}-{month}-{day} 没有检测到目标数据。")
# 日期加一天
current_date += datetime.timedelta(days=1)
# 设置输出目录
output_dir = "GOCI_TSS_Files"
os.makedirs(output_dir, exist_ok=True)
# 设置日期范围
start_date = datetime.date(2011, 12, 1) # 起始日期
end_date = datetime.date(2011, 12, 31) # 测试范围
# 设置目标后缀(如 .TSS.he5.zip 文件)
target_suffix = ".TSS.he5.zip"
# 执行监测并下载
monitor_and_download_files(start_date, end_date, output_dir, target_suffix=target_suffix)
# 设置日期范围
start_date = datetime.date(2011, 12, 1) # 起始日期
end_date = datetime.date(2011, 12, 31) # 测试范围
您可以自定义目标文件的后缀(如 .TSS.he5.zip或 .CDOM.he5.zip),只下载匹配后缀的文件。实现自动批量下载
核心代码
num = 0 # 初始化索引
while True:
try:
# 提取文件路径,动态替换索引
index0 = html.index(f'realPath" name="downloadVOList[{num}].realPath" value="') + len(f'realPath" name="downloadVOList[{num}].realPath" value="')
index1 = html.index('"', index0)
filename = html[index0:index1]
# 检查并处理文件
if filename.endswith(".TSS.he5.zip"):
file_url = f"http://kosc.kiost.ac.kr/{filename}"
all_files.append(file_url)
# 动态增加索引
num += 1
except ValueError:
# 超出文件范围,停止循环
print(f"所有文件读取完成,共检测到 {len(all_files)} 个文件。")
break
动态遍历文件索引 (num):
它试图从HTML内容中找到与 downloadVOList[{num}] 对应的文件路径。
每次递增 num 的值以尝试读取下一个文件的路径。
检查文件类型:
解析到文件路径后,通过 if filename.endswith(“.TSS.he5.zip”) 检查文件是否符合目标条件。
将符合条件的文件路径加入列表:
如果路径符合 .TSS.he5.zip 后缀,则将完整的URL构造并添加到 all_files 列表中。