主頁 > 知識庫 > python 爬取天氣網(wǎng)衛(wèi)星圖片

python 爬取天氣網(wǎng)衛(wèi)星圖片

熱門標(biāo)簽:聊城語音外呼系統(tǒng) 地圖標(biāo)注自己和別人標(biāo)注區(qū)別 打電話機(jī)器人營銷 商家地圖標(biāo)注海報(bào) 南陽打電話機(jī)器人 孝感營銷電話機(jī)器人效果怎么樣 ai電銷機(jī)器人的優(yōu)勢 騰訊地圖標(biāo)注沒法顯示 海外網(wǎng)吧地圖標(biāo)注注冊

項(xiàng)目地址:

https://github.com/MrWayneLee/weather-demo

代碼部分

下載生成文件功能

# 下載并生成文件
def downloadImg(imgDate, imgURLs, pathName):
    a,s,f = 0,0,0
    timeStart = time.time()
    while a  len(imgURLs):
        req = requests.get(imgURLs[a])
        imgName = str(imgURLs[a])[-13:-9]
        print(str("開始請求" + imgDate + " " + imgName + "的數(shù)據(jù)"))
        if req.status_code == 200:
            open(pathName + '\\' + os.path.basename(imgName) + '.png', 'wb').write(req.content)
            print("數(shù)據(jù)" + imgDate + " " + imgName + "下載完成")
            s += 1
            del req
        elif req.status_code == 404:
            print("數(shù)據(jù)" + imgDate + " " + imgName + "不存在")
            f += 1
        a += 1
    timeEnd = time.time()
    totalTime = round(timeEnd - timeStart, 2)
    print("全部數(shù)據(jù)請求完成!總耗時(shí):",totalTime,"秒")
    print("共請求", a, "次;成功", s, "次;失敗", f, "次")

創(chuàng)建文件夾

def createFolder(pathName):
    imgName_Year = pathName[0:4]
    imgName_Month = pathName[4:6]
    imgName_Day = pathName[6:8]
    imgName_date = imgName_Year + '-' + imgName_Month + '-' + imgName_Day

    mainPath = 'F:\\[Wayne Lee]\\學(xué)習(xí)資料\\Python\\爬取圖像'
    newPathName = mainPath + '\\' + imgName_date
    realPath = newPathName + '\\'

    isExists = os.path.exists(newPathName)

    if not isExists:
        os.makedirs(newPathName)
        print("新文件夾 [" + imgName_date + "] 創(chuàng)建成功")
        return realPath
    else:
        print(pathName + "文件夾已存在")
        return realPath

生成時(shí)間列表

def generateTime(imgUrl):
    timeList = []
    imgUrlList = []
    h,j = 0,0
    while h  24:
        m = 0
        while m  60:
            timeList.append("{:0>4d}".format(h * 100 + m))
            m += 15
        h += 1
    # print(timeList)
    # print(len(timeList))
    while j  len(timeList):
        imgUrlList.append(str(imgUrl + timeList[j] + "00000.JPG"))
        # print(timeList[j])
        j += 1
    return imgUrlList
    # print(imgUrlList)
    # print(len(imgUrlList))

生成下載URL列表

def downloadUrl(imgDate):
    imgUrl = "http://image.nmc.cn/product/" + imgDate[0:4] + "/" + imgDate[4:6] + "/" + imgDate[6:8] + "/WXBL/SEVP_NSMC_WXBL_FY4A_ETCC_ACHN_LNO_PY_" + imgDate # + "0000" +"00000.JPG"
    URLlist = list(generateTime(imgUrl))
    return URLlist

主函數(shù)

# 主函數(shù)
if __name__ == '__main__':
    # imgUrl = "http://image.nmc.cn/product/2020/04/11/WXBL/SEVP_NSMC_WXBL_FY4A_ETCC_ACHN_LNO_PY_20200411044500000.JPG"
    # imgUrl = "http://image.nmc.cn/product/2020/04/11/WXBL/SEVP_NSMC_WXBL_FY4A_ETCC_ACHN_LNO_PY_20200411"
    # imgName = imgUrl[-21:-9]

    while True:
        print("[1]手動(dòng)輸入日期")
        print("[2]獲取當(dāng)天日期")
        print("[3]退出程序")
        choose = str(input("你的選擇:"))
        if choose == "1":
            imgDate = str(input("請輸入日期[如20200411]:"))
            urlList = list(downloadUrl(imgDate))
            break
        elif choose == "2":
            imgDate = time.strftime("%Y%m%d",time.localtime())
            urlList = list(downloadUrl(imgDate))
            break
        elif choose == "3":
            break
        else:
            print("你的選擇有誤!請重試")

開始下載

    pathName = createFolder(imgDate)
    # 開始下載
    downloadImg(imgDate, urlList, pathName)

完整代碼

import requests
import time
import datetime
import os

# 下載并生成文件
def downloadImg(imgDate, imgURLs, pathName):
    a,s,f = 0,0,0
    timeStart = time.time()
    while a  len(imgURLs):
        req = requests.get(imgURLs[a])
        imgName = str(imgURLs[a])[-13:-9]
        print(str("開始請求" + imgDate + " " + imgName + "的數(shù)據(jù)"))
        if req.status_code == 200:
            open(pathName + '\\' + os.path.basename(imgName) + '.png', 'wb').write(req.content)
            print("數(shù)據(jù)" + imgDate + " " + imgName + "下載完成")
            s += 1
            del req
        elif req.status_code == 404:
            print("數(shù)據(jù)" + imgDate + " " + imgName + "不存在")
            f += 1
        a += 1
    timeEnd = time.time()
    totalTime = round(timeEnd - timeStart, 2)
    print("全部數(shù)據(jù)請求完成!總耗時(shí):",totalTime,"秒")
    print("共請求", a, "次;成功", s, "次;失敗", f, "次")

# 創(chuàng)建文件夾
def createFolder(pathName):
    imgName_Year = pathName[0:4]
    imgName_Month = pathName[4:6]
    imgName_Day = pathName[6:8]
    imgName_date = imgName_Year + '-' + imgName_Month + '-' + imgName_Day

    mainPath = 'F:\\[Wayne Lee]\\學(xué)習(xí)資料\\Python\\爬取圖像'
    newPathName = mainPath + '\\' + imgName_date
    realPath = newPathName + '\\'

    isExists = os.path.exists(newPathName)

    if not isExists:
        os.makedirs(newPathName)
        print("新文件夾 [" + imgName_date + "] 創(chuàng)建成功")
        return realPath
    else:
        print(pathName + "文件夾已存在")
        return realPath

# 生成時(shí)間列表
def generateTime(imgUrl):
    timeList = []
    imgUrlList = []
    h,j = 0,0
    while h  24:
        m = 0
        while m  60:
            timeList.append("{:0>4d}".format(h * 100 + m))
            m += 15
        h += 1
    # print(timeList)
    # print(len(timeList))
    while j  len(timeList):
        imgUrlList.append(str(imgUrl + timeList[j] + "00000.JPG"))
        # print(timeList[j])
        j += 1
    return imgUrlList
    # print(imgUrlList)
    # print(len(imgUrlList))

# 生成下載URL列表
def downloadUrl(imgDate):
    imgUrl = "http://image.nmc.cn/product/" + imgDate[0:4] + "/" + imgDate[4:6] + "/" + imgDate[6:8] + "/WXBL/SEVP_NSMC_WXBL_FY4A_ETCC_ACHN_LNO_PY_" + imgDate # + "0000" +"00000.JPG"
    URLlist = list(generateTime(imgUrl))
    return URLlist

# 主函數(shù)
if __name__ == '__main__':
    # imgUrl = "http://image.nmc.cn/product/2020/04/11/WXBL/SEVP_NSMC_WXBL_FY4A_ETCC_ACHN_LNO_PY_20200411044500000.JPG"
    # imgUrl = "http://image.nmc.cn/product/2020/04/11/WXBL/SEVP_NSMC_WXBL_FY4A_ETCC_ACHN_LNO_PY_20200411"
    # imgName = imgUrl[-21:-9]

    while True:
        print("[1]手動(dòng)輸入日期")
        print("[2]獲取當(dāng)天日期")
        print("[3]退出程序")
        choose = str(input("你的選擇:"))
        if choose == "1":
            imgDate = str(input("請輸入日期[如20200411]:"))
            urlList = list(downloadUrl(imgDate))
            break
        elif choose == "2":
            imgDate = time.strftime("%Y%m%d",time.localtime())
            urlList = list(downloadUrl(imgDate))
            break
        elif choose == "3":
            break
        else:
            print("你的選擇有誤!請重試")

    # 創(chuàng)建文件夾
    pathName = createFolder(imgDate)
    # 開始下載
    downloadImg(imgDate, urlList, pathName)

爬取效果

以上就是python 爬取天氣網(wǎng)衛(wèi)星圖片的詳細(xì)內(nèi)容,更多關(guān)于python 爬取天氣網(wǎng)圖片的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • 基于Python實(shí)現(xiàn)ComicReaper漫畫自動(dòng)爬取腳本過程解析
  • Python爬蟲實(shí)戰(zhàn)之爬取京東商品數(shù)據(jù)并實(shí)實(shí)現(xiàn)數(shù)據(jù)可視化
  • 高考要來啦!用Python爬取歷年高考數(shù)據(jù)并分析
  • Python爬蟲實(shí)戰(zhàn)之使用Scrapy爬取豆瓣圖片
  • 只用50行Python代碼爬取網(wǎng)絡(luò)美女高清圖片
  • 再也不用花錢買漫畫!Python爬取某漫畫的腳本及源碼

標(biāo)簽:迪慶 揚(yáng)州 撫州 六盤水 南寧 牡丹江 聊城 楊凌

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python 爬取天氣網(wǎng)衛(wèi)星圖片》,本文關(guān)鍵詞  python,爬取,天氣,網(wǎng),衛(wèi)星,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python 爬取天氣網(wǎng)衛(wèi)星圖片》相關(guān)的同類信息!
  • 本頁收集關(guān)于python 爬取天氣網(wǎng)衛(wèi)星圖片的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章