python 爬取天氣網(wǎng)衛(wèi)星圖片
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('開始請(qǐng)求' + 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 reqelif req.status_code == 404: print('數(shù)據(jù)' + imgDate + ' ' + imgName + '不存在') f += 1a += 1 timeEnd = time.time() totalTime = round(timeEnd - timeStart, 2) print('全部數(shù)據(jù)請(qǐng)求完成!總耗時(shí):',totalTime,'秒') print('共請(qǐng)求', 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 = 0while m < 60: timeList.append('{:0>4d}'.format(h * 100 + m)) m += 15h += 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('請(qǐng)輸入日期[如20200411]:')) urlList = list(downloadUrl(imgDate)) breakelif choose == '2': imgDate = time.strftime('%Y%m%d',time.localtime()) urlList = list(downloadUrl(imgDate)) breakelif choose == '3': breakelse: print('你的選擇有誤!請(qǐng)重試')
開始下載
pathName = createFolder(imgDate) # 開始下載 downloadImg(imgDate, urlList, pathName)
完整代碼
import requestsimport timeimport datetimeimport 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('開始請(qǐng)求' + 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 reqelif req.status_code == 404: print('數(shù)據(jù)' + imgDate + ' ' + imgName + '不存在') f += 1a += 1 timeEnd = time.time() totalTime = round(timeEnd - timeStart, 2) print('全部數(shù)據(jù)請(qǐng)求完成!總耗時(shí):',totalTime,'秒') print('共請(qǐng)求', 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 = 0while m < 60: timeList.append('{:0>4d}'.format(h * 100 + m)) m += 15h += 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('請(qǐng)輸入日期[如20200411]:')) urlList = list(downloadUrl(imgDate)) breakelif choose == '2': imgDate = time.strftime('%Y%m%d',time.localtime()) urlList = list(downloadUrl(imgDate)) breakelif choose == '3': breakelse: print('你的選擇有誤!請(qǐng)重試') # 創(chuàng)建文件夾 pathName = createFolder(imgDate) # 開始下載 downloadImg(imgDate, urlList, pathName)爬取效果
以上就是python 爬取天氣網(wǎng)衛(wèi)星圖片的詳細(xì)內(nèi)容,更多關(guān)于python 爬取天氣網(wǎng)圖片的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. html清除浮動(dòng)的6種方法示例2. jsp文件下載功能實(shí)現(xiàn)代碼3. Python基于tkinter canvas實(shí)現(xiàn)圖片裁剪功能4. 正則表達(dá)式匹配${key}并在Java中使用的詳細(xì)方法5. xpath簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理6. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案7. 阿里前端開發(fā)中的規(guī)范要求8. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟9. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐10. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法
