python下載衛(wèi)星云圖合成gif的方法示例
Python下載中央氣象臺(tái)衛(wèi)星云圖后保存為gif并播放,大致步驟:
- 獲取URL
- 下載圖片
- 合成GIF
- 播放GIF
1.獲取URL
1.1 先下載一份網(wǎng)頁源碼看看網(wǎng)頁結(jié)構(gòu)
保存為:response.txt
#http庫
import requests
#準(zhǔn)備http請(qǐng)求頭
headers = {"user-agent": "firefox"}
#中央氣象臺(tái)衛(wèi)星云圖網(wǎng)頁
url = 'http://www.nmc.cn/publish/satellite/fy2.htm'
#獲取網(wǎng)頁
r = requests.get(url, headers=headers)
#改編碼方式支持中文
r.encoding='utf-8'
#保存為文本
with open('response.txt','w', encoding='utf-8') as f:
f.write(r.text)
1.2 到網(wǎng)頁查看圖片鏈接
右鍵圖片---查看元素

圖片鏈接如下:可以看到圖片鏈接的域名和網(wǎng)頁域名不同。
1.3 在網(wǎng)頁碼源response.txt中搜索圖片名稱
發(fā)現(xiàn)有一處列出了動(dòng)畫的12張圖片:可以看到12張圖片的鏈接都在script字段中。

1.4 過濾出script,找到所有url
使用html解析庫解析出script,script的開頭type="text/javascript"作為過濾條件,結(jié)果打印看看:
#html/xml解析庫
from lxml import etree
#解析response
html = etree.HTML(r.text)
result = html.xpath('//script[@type="text/javascript"]/text()')[2]
print(result)
打印結(jié)果如下,可以看到是多行字符串。

根據(jù)圖片的鏈接規(guī)律,可以用正則匹配出來:
#正則庫
import re
urls = re.findall('/product.*.JPG', result)
print(urls)
成功匹配出圖片url。注意這里的url只有后半部分,根據(jù)之前的圖片鏈接可知,實(shí)際圖片url還需加上:http://image.mnc.cn。

1.5 因此寫獲取圖片URL函數(shù)
def getpage(page):
try:
r = requests.get(page, headers=headers)
html = etree.HTML(r.text)
result = html.xpath('//script[@type="text/javascript"]/text()')[2]
urls = re.findall('/product.*.JPG', result)
return urls
except Exception as e:
print(e)
2.下載圖片
拿到圖片url的列表后,就是下載圖片:
#url前綴
base_url = 'http://image.nmc.cn'
def dlpic(urls):
# 定義一個(gè)文件名稱收集列表
filenames = []
for item in urls:
r = requests.get(base_url + item, headers)
#文件名就是用斜杠把字符串分隔,取走后后一個(gè)字符串
filename = item.split('/')[-1]
filenames.append(filename)
#保存圖片
with open('wxyt_pic\\' + filename, 'wb') as f:
f.write(r.content)
print('已下載:'+item)
#返回文件名稱列表,用于合成gif
return filenames
3.合成圖片
# 圖片操作庫
import imageio
def makegif(images):
# 創(chuàng)建空列表,把圖片明反序
frames = []
images.reverse()
# 加載12張圖片
for item in images:
frames.append(imageio.imread('wxyt_pic\\'+item))
# 合成1張gif
imageio.mimsave('hecheng.gif', frames, 'GIF', duration=1)
4.播放圖片
def playgif(seq=0):
if set == 0:
#播放12張合成好的gif
animation = pyglet.resource.animation('hecheng.gif')
else:
pyglet.resource.path = ['wxyt_pic']
la = os.listdir('wxyt_pic')
images = []
for n in la:
images.append(pyglet.resource.image(n))
#播放庫存中的所有照片
animation = pyglet.image.Animation.from_image_sequence(images, period=0.5, loop=True)
#顯示動(dòng)畫
sprite = pyglet.sprite.Sprite(animation)
windows = pyglet.window.Window(width=sprite.width, height=sprite.height)
@windows.event
def on_draw():
windows.clear()
sprite.draw()
pyglet.app.run()
5.整體代碼
import requests
from lxml import etree
import imageio
import re
import pyglet
import os
# 在腳本同目錄下,新建一個(gè)文件夾,存儲(chǔ)當(dāng)天12張圖
def ckdir():
if os.path.exists('wxyt_pic') == False:
os.mkdir('wxyt_pic')
# 獲取圖片url列表
def getpage(page):
try:
r = requests.get(page, headers=headers)
html = etree.HTML(r.text)
result = html.xpath('//script[@type="text/javascript"]/text()')[2]
urls = re.findall('/product.*.JPG', result)
return urls
except Exception as e:
print(e)
# 下載圖片
def dlpic(urls):
filenames = []
for item in urls:
r = requests.get(base_url + item, headers)
filename = item.split('/')[-1]
filenames.append(filename)
with open('wxyt_pic\\' + filename, 'wb') as f:
f.write(r.content)
print('已下載:'+item)
return filenames
# 制作gif
def makegif(images):
frames = []
images.reverse()
for item in images:
frames.append(imageio.imread('wxyt_pic\\'+item))
imageio.mimsave('hecheng.gif', frames, 'GIF', duration=1)
# 播放gif
def playgif(seq=0):
if set == 0:
#播放12張合成好的gif
animation = pyglet.resource.animation('hecheng.gif')
else:
pyglet.resource.path = ['wxyt_pic']
la = os.listdir('wxyt_pic')
images = []
for n in la:
images.append(pyglet.resource.image(n))
#播放庫存中的所有照片
animation = pyglet.image.Animation.from_image_sequence(images, period=0.5, loop=True)
#顯示動(dòng)畫
sprite = pyglet.sprite.Sprite(animation)
windows = pyglet.window.Window(width=sprite.width, height=sprite.height)
@windows.event
def on_draw():
windows.clear()
sprite.draw()
pyglet.app.run()
# init
if __name__ == '__main__':
base_url = 'http://image.nmc.cn'
page = 'http://www.nmc.cn/publish/satellite/fy2.htm'
headers = {"user-agent": "firefox"}
ckdir()
urls = getpage(page)
images = dlpic(urls)
makegif(images)
# 0只播放今天12張,1播放庫存里所有照片
playgif(1)
6.最終效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python capitalize()函數(shù)的用法詳解
在Python中,capitalize()將字符串的第一個(gè)字符轉(zhuǎn)換為大寫字母,并將所有其他字符(如果有的話)轉(zhuǎn)換為小寫,本文就將給大家介紹一下Python capitalize()函數(shù)的使用方法,感興趣的朋友跟著小編一起來看看吧2023-07-07
Python基于最小二乘法實(shí)現(xiàn)曲線擬合示例
這篇文章主要介紹了Python基于最小二乘法實(shí)現(xiàn)曲線擬合,涉及Python基于numpy及scipy庫進(jìn)行曲線擬合操作相關(guān)運(yùn)算技巧,需要的朋友可以參考下2018-06-06
python調(diào)用win32接口進(jìn)行截圖的示例
這篇文章主要介紹了python調(diào)用win32接口進(jìn)行截圖的示例,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11
Python學(xué)習(xí)之shell腳本的使用詳解
這篇文章主要為大家分析一個(gè)python庫–sh(系統(tǒng)調(diào)用),主要內(nèi)容包括其使用實(shí)例、應(yīng)用技巧、基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下2023-04-04
Python2 與Python3的版本區(qū)別實(shí)例分析
這篇文章主要介紹了Python2 與Python3的版本區(qū)別,結(jié)合實(shí)例形式分析了Python2 與Python3的版本使用過程中的各種常見區(qū)別、用法與注意事項(xiàng),需要的朋友可以參考下2020-03-03

