python 爬虫抓取中央气象台-台风网

python 爬虫抓取中央气象台-台风网

技术交流: 1548357515

获取某一年的台风名称数据

请求链接为:
http://typhoon.nmc.cn/weatherservice/typhoon/jsons/list_2019?t=1614158017458&callback=typhoon_jsons_list_2019
t: 13位时间戳 callback: t yphoon_jsons_list + 年份
返回数据中文有乱码不要担心, 代码中没事

# 获取所有台风
def get_html(url):
    html_obj = requests.get(url, headers=headers(url), proxies=proxys(), verify=False).text
    date = json.loads(re.match(".*?({.*}).*", html_obj, re.S).group(1))['typhoonList']
    item_list = []
    for v in date:
        item = {}
        item['id'] = v[0]
        item['name'] = '%s%s%s' % (v[4], v[2], v[1])
        item['dec'] = '%s' % v[6]
        item_list.append(item)
    return item_list

获取单个台风的详细数据

链接位:
http://typhoon.nmc.cn/weatherservice/typhoon/jsons/view_2540445?t=1614158670558&callback=typhoon_jsons_view_2540445
t: 13位时间戳 callback: typhoon_jsons_view_ + 台风的id

在这里插入图片描述

def get_xiang(item):
    print("开始抓取%s 台风信息, id: %s" % (item['name'], item['id']))
    t = int(round(time.time() * 1000))
    url = 'http://typhoon.nmc.cn/weatherservice/typhoon/jsons/view_%s?t=%s&callback=typhoon_jsons_view_2297801' % (item['id'], t)
    html_obj = requests.get(url, headers=headers(url), proxies=proxys(), verify=False).text
    date = json.loads(re.match(".*?({.*}).*", html_obj, re.S).group(1))['typhoon']
    # 建立字典
    info_dicts = defaultdict(list)
    for v in date[8]:
        info_dicts['id'].append(item['id'])
        info_dicts['name'].append(item['name'])
        info_dicts['desc'].append(item['dec'])
        # 时间  时间戳转日期
        info_dicts['时间'].append(millisecond_to_time(v[2]))
        info_dicts['风速'].append('%sm/s' % v[7])
        yi = '%s' % v[8]
        # 东:East,缩写成E; 2、南:South,缩写成S; 3、西:West,缩写成W; 4、北:North
        info_dicts['移向'].append(yi.replace('N', '北').replace('E', '东').replace('S', '南').replace('W', '西'))
        # 强度
        info_dicts['强度'].append(get_type(v[3]))
        info_dicts['中心位置'].append('%sN/%sE' % (v[5], v[4]))
        info_dicts['中心气压'].append('%s百帕' % v[6])
    data = pd.DataFrame(info_dicts)
    return data
# 强度类型
def get_type(date_type):
    item = {'TC': '热带气旋', 'TD': '热带低压', 'TS': '热带风暴', 'STS': '强热带风暴',
            'TY': '台风', 'STY': '强台风', 'SuperTY': '超强台风', '': '',}
    return item.get(date_type, '')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值