python爬取中国台风网台风轨迹点数据

  1. 首先进入中国台风网,按F12,打开开发者工具,在台风列表中选择我们所需要查看的台风:
    python爬取中国台风网台风轨迹点数据_第1张图片
  2. 在开发者工具中查看,获取台风数据的请求URL:
    python爬取中国台风网台风轨迹点数据_第2张图片
  3. 观察url的构成,注意这里的url含有时间戳,应该注意这一点,在拼接url时加入当前时间戳。
    python爬取中国台风网台风轨迹点数据_第3张图片
    台风轨迹点数据链接
    http://d1.weather.com.cn/typhoon/typhoon_data/2018/1822.json?callback=getData&_=1538966382029
    具体代码如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#name:typhoon,根据台风的年份与编号获取台风轨迹点 (完整版,可以成功运行)
import requests
import json
import time
import xlwt
year = input("输入年份:") # 输入台风的年份
number = input("输入编号:") # 输入台风的编号
#Python爬虫天气预报(小白入门) | Coder·码农网  https://www.codercto.com/a/9087.html
#含时间戳的url,请求头
url = "http://d1.weather.com.cn/typhoon/typhoon_data/"+year+"/"+number+".json?callback=getData&_="+str(int(round((time.time()*1000))))
headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36",
        "Referer": "http://typhoon.weather.com.cn/gis/typhoon_p.shtml",
    }
r = requests.get(url,headers=headers)
print(r.text)
a = json.loads(r.text[8:-1])#解析json文件
print(a["typhoon"][8])
print(len(a["typhoon"][8]))
#台风轨迹点
start_time = [] #台风到达时间
lon = [] #台风到达地经度
lat = [] #台风到达地纬度
central_pressure = [] #台风中心气压
wind = [] #台风风速风力(米/秒)
direction = [] #未来移向
feature_speed = [] #未来移速
#台风登陆时的轨迹点
start_time_1 = [] #台风到达时间
lon_1 = [] #台风到达地经度
lat_1 = [] #台风到达地纬度
central_pressure_1 = [] #台风中心气压
wind_1 = [] #台风风速风力(米/秒)
direction_1 = [] #未来移向
feature_speed_1 = [] #未来移速
denglu = [] #台风登陆描述
print(a["typhoon"][8][21])
for i in range(len(a["typhoon"][8])):
    #b = a["typhoon"][8][i]
    #print(b[1],b[4],b[5],b[6],b[7],b[8],b[9])
    start_time.append(a["typhoon"][8][i][1])
    lon.append(a["typhoon"][8][i][4])
    lat.append(a["typhoon"][8][i][5])
    central_pressure.append(a["typhoon"][8][i][6])
    wind.append(a["typhoon"][8][i][7])
    direction.append(a["typhoon"][8][i][8])
    feature_speed.append(a["typhoon"][8][i][9])
    if len(a["typhoon"][8][i]) > 13:
        start_time_1.append(a["typhoon"][8][i][1])
        lon_1.append(a["typhoon"][8][i][4])
        lat_1.append(a["typhoon"][8][i][5])
        central_pressure_1.append(a["typhoon"][8][i][6])
        wind_1.append(a["typhoon"][8][i][7])
        direction_1.append(a["typhoon"][8][i][8])
        feature_speed_1.append(a["typhoon"][8][i][9])
        denglu.append(a["typhoon"][8][i][13])
        print(a["typhoon"][8][i][13])


f = xlwt.Workbook() #创建工作簿
sheet1 = f.add_sheet(u'sheet1',cell_overwrite_ok=True) #创建sheet,存放台风轨迹点(包括台风登陆时的轨迹点)
sheet2 = f.add_sheet(u'sheet2',cell_overwrite_ok=True) #创建sheet,存放台风登陆时的轨迹点
j=0
q=0
for start_time1,lon1,lat1,central_pressure1,wind1,direction1,feature_speed1\
     in zip(start_time,lon,lat,central_pressure,wind,direction,feature_speed):
    sheet1.write(j,0,start_time1);sheet1.write(j,1,lon1);sheet1.write(j,2,lat1);sheet1.write(j,3,central_pressure1);sheet1.write(j,4,wind1);sheet1.write(j,5,direction1);sheet1.write(j,6,feature_speed1);
    j = j+1
for start_time1,lon1,lat1,central_pressure1,wind1,direction1,feature_speed1,denglu1\
     in zip(start_time_1,lon_1,lat_1,central_pressure_1,wind_1,direction_1,feature_speed_1,denglu):
    sheet2.write(q,0,start_time1);sheet2.write(q,1,lon1);sheet2.write(q,2,lat1);sheet2.write(q,3,central_pressure1);sheet2.write(q,4,wind1);sheet2.write(q,5,direction1);sheet2.write(q,6,feature_speed1);\
    sheet2.write(q,7,denglu1);
    q = q+1
f.save(r'路径\1409.xls' )#保存文件,文件名每一个台风更换不同的名称,我这里是以台风编号命名的。这个路径自己设定


你可能感兴趣的:(爬虫)