python 爬取天气网卫星图片
作者:MrWayneLee 时间:2022-01-11 15:52:48
项目地址:
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)
爬取效果
来源:https://github.com/MrWayneLee/weather-demo
标签:python,爬虫,天气网,图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python面向对象三大特征 封装、继承、多态
2022-01-09 04:31:51
![](https://img.aspxhome.com/file/2023/1/92501_0s.png)
Python中的heapq模块源码详析
2023-09-23 12:07:23
![](https://img.aspxhome.com/file/2023/5/88035_0s.jpg)
IE8 在元素尺寸大于(2048px/4096px)时 alpha滤镜渲染失败
2010-01-05 16:39:00
css+JavaScript实现PDF、ZIP、DOC链接的标注
2007-05-11 17:03:00
使用python图形模块turtle库绘制樱花、玫瑰、圣诞树代码实例
2022-07-11 05:28:15
![](https://img.aspxhome.com/file/2023/2/72852_0s.png)
Python2.x中文乱码问题解决方法
2023-09-20 14:35:35
![](https://img.aspxhome.com/file/2023/4/105084_0s.png)
一篇文章搞懂Python反斜杠的相关问题
2021-11-26 17:45:19
![](https://img.aspxhome.com/file/2023/2/97662_0s.png)
php生成curl命令行的方法
2023-07-23 22:19:42
DataFrame.to_excel多次写入不同Sheet的实例
2022-03-26 01:20:14
Python实现自动化处理Word文档的方法详解
2022-05-24 00:33:59
![](https://img.aspxhome.com/file/2023/2/72782_0s.jpg)
PyQt5组件读取参数的实例
2023-04-02 07:47:29
Go 语言实现 HTTP 文件上传和下载
2023-06-23 01:42:24
thinkphp3.x连接mysql数据库的方法(具体操作步骤)
2023-11-22 20:04:41
关于Keras Dense层整理
2022-03-02 17:04:15
python pygame实现滚动横版射击游戏城市之战
2021-07-11 00:32:51
![](https://img.aspxhome.com/file/2023/2/83992_0s.jpg)
Django中select_related和prefetch_related的用法与区别详解
2023-10-08 12:38:08
![](https://img.aspxhome.com/file/2023/9/99409_0s.jpg)
Python中DataFrame判断两列数据是否相等的方法
2023-12-09 10:17:37
Django drf请求模块源码解析
2023-06-07 10:03:07
![](https://img.aspxhome.com/file/2023/9/95819_0s.jpg)
SQL Server中导入导出数据的三种方式
2008-11-28 15:53:00
CSS框架/命名/规则 注意要点
2008-06-03 13:07:00