APIが混雑している際に再取得するdef fetch_url(url):を追加
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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import pandas as pd import plotly.offline as pyo import plotly.graph_objs as go import urllib.request from time import sleep from datetime import datetime, date, timedelta from dateutil.relativedelta import relativedelta import ssl ssl._create_default_https_context = ssl._create_unverified_context #パラメーターの作成 #trade data type C Commodities S Services datatype = "type=C" #china=156,UK=826,US=842,jp=392or all reporting_area = "&r=842" #Annual=A,Monthly=M frequency = "&freq=M" #YYYY or YYYYMM or now or recent time_period = "&ps=201201" # Harmonized System (HS) classification = "&px=H4" #china=156,UK=826,US=842,or all partner_area = "&p=all" # 1 (imports) and 2 (exports), import_or_export = "&rg=1" #HS CODE classification_code = "&cc=610910" #csv or json fmt = "&fmt=csv" #取得する相手国の数 amount_country = 6 #取得期間(年or月) term=72 # term = 72 #一番古いデータの年月(更新あり) oldest = "201201" url = ("https://comtrade.un.org/api/get?"+datatype + reporting_area+frequency+time_period+classification+partner_area+ import_or_export+classification_code+fmt) #csvをdfに入れる res = urllib.request.urlopen(url) df = pd.read_csv(res) #最新年を取得 newest = (df["Period"].max()) #最新年のデータのみを取得 df_n = df[df["Period"] == newest] #valueが高い順に並べ替え df_s = df_n.sort_values('Trade Value (US$)', ascending=False) #上位10件を取得してリスト化 partner = [] for var in range(1, amount_country): partner.append(df_s.iat[var, 12]) #ここに過去の日付のcsvを追加するコードを入れる def get_oldtime(tdatetime): tdatetime = datetime.strptime(str(oldest),"%Y%m") tdatetime =(tdatetime + relativedelta(months=1)) return tdatetime.strftime('%Y%m') #一か月ごとのパラメータを生成 def add_old(old): addurl = ("https://comtrade.un.org/api/get?"+datatype + reporting_area+frequency+old+classification+partner_area+ import_or_export+classification_code+fmt) sleep(2) return addurl #APIが混雑している際に再取得する def fetch_url(url): max_times=10 sleep_sec=5 retry_count = 0 while True: f = urllib.request.urlopen(url) try: retry_count += 1 if f.getcode() == 200 or retry_count >= max_times: # 200が返ってくるか、最大試行回数に到達した場合ループを抜ける return f break time.sleep(sleep_sec) except ValueError: print("Oops! That was no valid number. Try again...") for i in range(1,term): oldest = get_oldtime(oldest) time_period = "&ps=" + str(oldest) added_url = add_old(time_period) added_res = fetch_url(added_url) print(added_res.getcode()) added_df = pd.read_csv(added_res) df = pd.concat([df,added_df]) print(oldest) data = [] for i in partner: df2 = df[df["Partner"] == i] trace = go.Scatter(x=df2["Period Desc."], y=df2["Trade Value (US$)"], mode = 'lines', name = i) data.append(trace) layout = go.Layout( title="アメリカが輸入するTシャツ等HS6109.10該当貨物" ) fig = go.Figure(data=data, layout=layout) pyo.plot(fig,filename="ustshirt.html") |
コメントを残す