通过Python下载星历数据
星历数据来源 https://celestrak.org/
其他星历搜索网站https://www.n2yo.com/ , http://rod.sladen.org.uk/
前提:安装python环境
1.控制台输入指令
pip install requests
C:\Users\Dexter>pip install requests
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: requests in c:\users\dexter\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (2.32.3)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\dexter\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from requests) (3.4.0)
Requirement already satisfied: idna<4,>=2.5 in c:\users\dexter\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from requests) (3.10)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\dexter\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from requests) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\dexter\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (from requests) (2024.12.14)
C:\Users\Dexter>
2.新建文件urls.txt,粘贴以下内容
https://celestrak.org/NORAD/elements/gp.php?GROUP=last-30-days&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=stations&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=visual&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=active&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=analyst&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=cosmos-1408-debris&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=fengyun-1c-debris&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=iridium-33-debris&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=cosmos-2251-debris&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=weather&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=noaa&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=goes&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=resource&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=sarsat&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=dmc&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=tdrss&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=argos&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=planet&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=spire&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=geo&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?SPECIAL=gpz&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?SPECIAL=gpz-plus&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=intelsat&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=ses&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=eutelsat&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=iridium&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=iridium-NEXT&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=starlink&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=oneweb&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=orbcomm&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=globalstar&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=swarm&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=amateur&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=satnogs&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=x-comm&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=other-comm&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=gorizont&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=raduga&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=molniya&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=gnss&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=gps-ops&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=glo-ops&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=galileo&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=beidou&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=sbas&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=nnss&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=musson&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=science&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=geodetic&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=engineering&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=military&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=radar&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=cubesat&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=other&FORMAT=tle
https://celestrak.org/NORAD/elements/gp.php?GROUP=education&FORMAT=tle
3.新建文件eph.py
"""
按类别下载星历
http://celestrak.org网站取消了直接下载全部类别星历的功能,改为只能按分类下载
因此将各分类的url写入到url.txt中,然后逐一下载、合并写入到ephs.txt文件中
"""
import requests
import sys
def eph(ephUrl):
session = requests.session()
rep = session.get(ephUrl)
return rep.status_code == 200, rep.text
def clean():
with open(r'ephs.txt', 'r') as f:
source = f.readlines()
if not source or len(source) < 1:
return
num = int(len(source) / 3) # 总行数/3=卫星数量
dest = source[:3] # 前3行(第一颗星)认为是不重复的
for i in range(1, num):
sat = source[i * 3] # 卫星名称行
line1 = source[i * 3 + 1] # 第一行tle
line2 = source[i * 3 + 2] # 第二行tle
if dest.count(line1) == 0: # 不是重复的卫星星历
dest.append(sat)
dest.append(line1)
dest.append(line2)
with open('ephs.txt', 'w') as f: # 保存到文件
f.writelines(dest)
if __name__ == '__main__':
with open('url.txt', 'r') as f:
urls = f.readlines()
if not urls or len(urls) < 1:
print('没有找到星历下载URL列表')
sys.exit()
mode = 'w' # 初次打开文件时使用覆盖方式
for url in urls:
one = url.replace('\n', '')
success, text = eph(one)
print(f'完成{one}下载,{success}')
if success:
# python的write方法会把\r转换成\n,导致文件生成空行,因此先去除\r,再去除多余的\n
while text.count('\r') > 0:
text = text.replace('\r', '')
while text.count('\n\n') > 0:
text = text.replace('\n\n', '\n')
with open(r'ephs.txt', mode) as f:
f.write(text)
if not text.endswith('\n'): # 确保有换行符
f.write('\n')
mode = 'a' # 后续打开文件使用追加方式
else:
print(f'下载星历列表失败:{url}')
# 清理重复数据
clean()
在控制台执行 eph.py指令
python eph.py