利用Python获取地球实时卫星照片最详细攻略,附代码。
环境
:Python3卫星
:向日葵8号主页卫星接口
:http://himawari8-dl.nict.go.jp/himawari8/img/D531106/latest.json
注:2020/02/27为日期,0550为格林威治时间
http://himawari8-dl.nict.go.jp/himawari8/img/D531106/thumbnail/550/2020/02/27/055000_0_0.png
注:20200227为日期
http://himawari8-dl.nict.go.jp/himawari8/movie/720/20200227_pifd.mp4
http://himawari8-dl.nict.go.jp/himawari8/movie/720/coastline/20200227_pifd.mp4
import datetime
import gc
import time
import urllib.request
from PIL import Image, ImageFile
from retrying import retry
def getDate():
localTime = datetime.datetime.now() - datetime.timedelta(minutes=30)
zeroTime = localTime - datetime.timedelta(hours=8)
localDate = "{}年{}月{}日\n {}时{}分00秒" \
.format(localTime.year, '0' * (2 - len(str(localTime.month))) + str(localTime.month),
'0' * (2 - len(str(localTime.day))) + str(localTime.day),
'0' * (2 - len(str(localTime.hour))) + str(localTime.hour),
str(localTime.minute // 10) + '0' * (2 - len(str(localTime.minute // 10))))
zeroDate = {'year': str(zeroTime.year), 'month': '0' * (2 - len(str(zeroTime.month))) + str(zeroTime.month),
'day': '0' * (2 - len(str(zeroTime.day))) + str(zeroTime.day),
'hour': '0' * (2 - len(str(zeroTime.hour))) + str(zeroTime.hour),
'minute': str(zeroTime.minute // 10) + '0' * (2 - len(str(zeroTime.minute // 10)))}
return localDate, zeroDate
def getURL(multiple, zeroDate, i, j):
rawURL = 'http://himawari8-dl.nict.go.jp/himawari8/img/D531106/{}d/550/{}/{}/{}/{}{}00_{}_{}.png'
earthURL = rawURL.format(multiple, zeroDate['year'], zeroDate['month'], zeroDate['day'], zeroDate['hour'],
zeroDate['minute'], i, j)
return earthURL
def downloadImg(multiple, zeroDate):
earth = Image.new('RGB', (multiple * 550, multiple * 550))
for i in range(multiple):
for j in range(multiple):
URL = getURL(multiple, zeroDate, i, j)
print('{}/{} picture started to download\n'
' URL->{}'.format(i * multiple + j + 1, pow(multiple, 2), URL))
image = down(URL)
f = ImageFile.Parser()
f.feed(image)
tempImg = f.close()
earth.paste(tempImg, (i * 550, j * 550, (i + 1) * 550, (j + 1) * 550))
print('{}/{} picture has been download'.format(i * multiple + j + 1, pow(multiple, 2)))
return earth
# @retry(wait_random_min=0, wait_random_max=3000)
@retry
def down(URL):
print(' Try')
headers = {'Connection': 'Keep-Alive',
'Accept': 'text/html, application/xhtml+xml, */*',
'Accept-Language': 'en-US,en;q=0.8,zh-Hans-CN;q=0.5,zh-Hans;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
}
req = urllib.request.Request(URL, headers=headers)
image = urllib.request.urlopen(req, timeout=1).read()
return image
def start():
# multiple 可以是 1, 2, 4, 8, 16, 20,数字越大图片质量越好
multiple = 8
localDate, zeroDate = getDate()
earth = downloadImg(multiple, zeroDate)
return earth
if __name__ == '__main__':
while True:
try:
a = time.time()
gc.disable()
earth = start()
earth.save('earth.jpg')
gc.enable()
b = time.time()
print('{}s'.format(b - a))
except Exception as e:
print(e)
finally:
gc.collect()
c = time.time()
print('{}s'.format(c - b))
print('------picture has been saved------')
print('Start waiting')
time.sleep(300)