python下載衛(wèi)星云圖合成gif的方法示例
Python下載中央氣象臺衛(wèi)星云圖后保存為gif并播放,大致步驟:
- 獲取URL
- 下載圖片
- 合成GIF
- 播放GIF
1.獲取URL
1.1 先下載一份網頁源碼看看網頁結構
保存為:response.txt
#http庫
import requests
#準備http請求頭
headers = {"user-agent": "firefox"}
#中央氣象臺衛(wèi)星云圖網頁
url = 'http://www.nmc.cn/publish/satellite/fy2.htm'
#獲取網頁
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 到網頁查看圖片鏈接
右鍵圖片---查看元素

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

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

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

1.5 因此寫獲取圖片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)
2.下載圖片
拿到圖片url的列表后,就是下載圖片:
#url前綴
base_url = 'http://image.nmc.cn'
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)
#返回文件名稱列表,用于合成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)
#顯示動畫
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
# 在腳本同目錄下,新建一個文件夾,存儲當天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)
#顯示動畫
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.最終效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Python使用keras和tensorflow遇到的問題及解決
這篇文章主要介紹了Python使用keras和tensorflow遇到的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
selenium3.0+python之環(huán)境搭建的方法步驟
這篇文章主要介紹了selenium3.0+python之環(huán)境搭建的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02

