python 爬取天气网卫星图片
程序员文章站
2022-03-18 17:37:44
项目地址:https://github.com/mrwaynelee/weather-demo代码部分下载生成文件功能# 下载并生成文件def downloadimg(imgdate, imgurls...
项目地址:
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 + "的数据")) if req.status_code == 200: open(pathname + '\\' + os.path.basename(imgname) + '.png', 'wb').write(req.content) print("数据" + imgdate + " " + imgname + "下载完成") s += 1 del req elif req.status_code == 404: print("数据" + imgdate + " " + imgname + "不存在") f += 1 a += 1 timeend = time.time() totaltime = round(timeend - timestart, 2) print("全部数据请求完成!总耗时:",totaltime,"秒") print("共请求", a, "次;成功", s, "次;失败", f, "次")
创建文件夹
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]\\学习资料\\python\\爬取图像' newpathname = mainpath + '\\' + imgname_date realpath = newpathname + '\\' isexists = os.path.exists(newpathname) if not isexists: os.makedirs(newpathname) print("新文件夹 [" + imgname_date + "] 创建成功") return realpath else: print(pathname + "文件夹已存在") return realpath
生成时间列表
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
主函数
# 主函数 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]手动输入日期") print("[2]获取当天日期") 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 + "的数据")) if req.status_code == 200: open(pathname + '\\' + os.path.basename(imgname) + '.png', 'wb').write(req.content) print("数据" + imgdate + " " + imgname + "下载完成") s += 1 del req elif req.status_code == 404: print("数据" + imgdate + " " + imgname + "不存在") f += 1 a += 1 timeend = time.time() totaltime = round(timeend - timestart, 2) print("全部数据请求完成!总耗时:",totaltime,"秒") print("共请求", a, "次;成功", s, "次;失败", f, "次") # 创建文件夹 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]\\学习资料\\python\\爬取图像' newpathname = mainpath + '\\' + imgname_date realpath = newpathname + '\\' isexists = os.path.exists(newpathname) if not isexists: os.makedirs(newpathname) print("新文件夹 [" + imgname_date + "] 创建成功") return realpath else: print(pathname + "文件夹已存在") return realpath # 生成时间列表 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 # 主函数 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]手动输入日期") print("[2]获取当天日期") 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)
爬取效果
以上就是python 爬取天气网卫星图片的详细内容,更多关于python 爬取天气网图片的资料请关注其它相关文章!
上一篇: 在Python中如何使用yield
下一篇: Java并发编程之同步容器与并发容器详解