Python脚本下台风数据

从浙江水利厅的台风路径网下的,但是数据和中国气象网是一样的。

import requests
import json
import os
from urllib import parse as URL_PARSE

url_search = r"http://typhoon.zjwater.gov.cn/Api/TyphoonSearch/"
url_info   = r"http://typhoon.zjwater.gov.cn/Api/TyphoonInfo/"
output_path= r"./"

def getTYinfo(str):
    r = requests.get(url_info + str[:6])
    if r.status_code == 200:
        data = r.json()
        savePath = os.path.join(output_path + str + ".json")
        print("Typhoon Info ", str, "get, save in " + savePath)
    else:
        print("Typhoon Info ", str, "download failed.")
    
def main():
    s = input("Input search information: ")
    if not s:
        print("Incorrect input. Quiting")
        return
    r = requests.get(url_search + URL_PARSE.quote(s, encoding="GBK"))
    info = r.json()
    for i, sg  in enumerate(info):
        print(i, sg['name'])
    s = input("Select ids(seperate with [Space]):").split()
    spl = [int(v) for v in s]
    for i in spl:
        try:
            getTYinfo(info[i]['name'])
        except Exception as e:
            pass

if __name__ == "__main__":
    main()

发表评论

电子邮件地址不会被公开。