服务器之家

服务器之家 > 正文

python 爬取天气网卫星图片

时间:2021-11-25 12:01     来源/作者:MrWayneLee

项目地址:

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

代码部分

下载生成文件功能

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 下载并生成文件
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, "次")

创建文件夹

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

生成时间列表

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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列表

?
1
2
3
4
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

主函数

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 主函数
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("你的选择有误!请重试")

开始下载

?
1
2
3
pathname = createfolder(imgdate)
# 开始下载
downloadimg(imgdate, urllist, pathname)

完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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 爬取天气网图片的资料请关注服务器之家其它相关文章!

原文链接:https://github.com/MrWayneLee/weather-demo

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部