台風経路1-python 3.7.0を通じて台風経路の履歴データを取得し、postgresqlデータベースに挿入する.


1.ページを登る http://typhoon.zjwater.gov.cn/default.aspx
F 12履歴の軌跡点に対応するurlを見つける.
2.pythonを通じてデータを取得し、postgresqlデータベースに挿入します.
import requests
import json
import  psycopg2
import cx_Oracle

url = "http://typhoon.zjwater.gov.cn/Api/TyphoonInfo/201911?callback=jQuery1830020370286197575638_1566524188610&_=1566524188868"

req_header = {
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36" ,
        "Referer": "http://typhoon.zjwater.gov.cn/default.aspx"
    }
r = requests.get(url, headers=req_header)
content_str = r.text[44:-3]
content_json = json.loads(content_str) #      json  
# print(content_json)
centerlat = content_json["centerlat"]
centerlng = content_json["centerlng"]
endtime = content_json["endtime"]
name = content_json["name"]
db = psycopg2.connect(database="postgis", user="postgres", password="postgres", host="127.0.0.1", port="5432")
print("      ")
cursor = db.cursor()
sql_del = "delete  from typhoon"
cursor.execute(sql_del)
db.commit()
for point in content_json["points"]:
    lat = point["lat"]#  
    lng = point["lng"]#  
    movedirection = point["movedirection"]#  
    movespeed = point["movespeed"]#  
    power = point["power"]#   
    pressure = point["pressure"]#    
    speed = point["speed"]  #  
    strong = point["strong"]#    
    #    cursor()            cursor
    sql_insert = "insert into typhoon (centerlat,centerlng,name,lng,lat,movedirection,movespeed,power,pressure,speed,strong) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
    params = (centerlat, centerlng, name, lng, lat, movedirection, movespeed, power, pressure, speed, strong)
    #    execute()       SQL   
    cursor.execute(sql_insert, params)
    db.commit()
print("      ")
3.最終データ
台风路径1-通过python3.7.0获取台风路径历史轨迹点数据并插入到postgresql数据库中_第1张图片