LoginSignup
4
2

More than 5 years have passed since last update.

貿易統計をチェックしよう

Last updated at Posted at 2017-09-11

貿易統計は、日本で1番アクセス数が多い統計です。
税関別まで細かく集計してあり、面白いのですが、困ったことに、品目別にみたとき、税関別の集計と全国の集計はかなり異なります。1年間で、460億円くらい異なる品目があります。でも、しょうがないのです。
よくある質問の25にあるように、「営業上の秘密が開示されることとなる場合には、法令に従い当該数量及び金額を明らかにしないことが可能です。」

それで、税関別と全国でどのくらい違いがあるかをチェックするツールをつくりました。
検証用のデータは nysol でさくっとだしてあります。

HS code 000000099 は中身不明なので、2017.7 の 271500000 アスファルトの輸出を検証します。

month,exp_imp,hs9,custom,ym,diff%0nr
03,2,000000099,120908370,115526438,5381932
05,2,000000099,90818053,86161077,4656976
07,1,271500000,4389811,418628,3971183
07,1,270400000,1043325,4856374,3813049
01,2,000000099,98350727,94579057,3771670
06,2,000000099,115329286,111587594,3741692
07,2,000000099,87160101,83640517,3519584
03,2,271500000,3217876,5196,3212680
04,1,392690000,20736504,17827751,2908753
04,1,390690100,6832529,9741282,2908753
06,1,390690100,6748723,9471058,2722335
06,1,392690000,20408903,17686568,2722335
04,1,271500000,2969493,293092,2676401
02,2,000000099,90102507,87456945,2645562
03,1,390690100,7079769,9712735,2632966
03,1,392690000,20455226,17822260,2632966
04,2,000000099,84141728,81518058,2623670
04,1,270400000,1637355,4006860,2369505
05,2,271500000,2360699,7438,2353261

selenium,BeautifulSoup + PhantomJS を使います。実際に使う場合にはインストールしてください。

exp_imp=1 # 1 輸出 2 輸入
year=2017
month='07' # ゼロ補完の月 使うときは、整数に戻す
hs9='271500000' #アスファルト
custom_total,total = check_custom_total(exp_imp,year,month,hs9)
税関別、全国
(4389811, 418628)
なんで、税関別の方が多くなるのだろう。不思議です。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
import time
from bs4 import BeautifulSoup
def check_custom_total(exp_imp,year,month,hs9):

    url ='http://www.customs.go.jp/toukei/srch/index.htm?M=09&P=1,{exp_imp},,,,,,,,1,0,'
    url = url + '{year},0,{month},0,2,{hs9},,,,,,,,,,1,,,,,,,,,,,,,,,,,,,,,,200'
    print(url.format(exp_imp=exp_imp,year=year,month=month,hs9=hs9))
    driver = webdriver.PhantomJS()
    driver.implicitly_wait(5) 
    driver.get(url.format(exp_imp=exp_imp,year=year,month=month,hs9=hs9))
    time.sleep(3)
    driver.switch_to_frame('FR_M_INFO')
    trs = BeautifulSoup(driver.page_source,'lxml').find_all('table')[2].find_all('tr')
    rsl={}
    r_total = 0
    for tr in trs[3:]:
        sTotal = tr.find('td',class_='left_sTotal')
        if sTotal is not None :
            rsl[sTotal.text[1:4]] ={}
            rsl[sTotal.text[1:4]]['country'] = sTotal.text[5:-3]
            rsl[sTotal.text[1:4]]['Value'] = tr.find_all('td')[5].text
            try:
                rsl[sTotal.text[1:4]]['Value'] = int(rsl[sTotal.text[1:4]]['Value'])
            except:
                rsl[sTotal.text[1:4]]['Value'] = 0
            r_total = r_total + rsl[sTotal.text[1:4]]['Value']
    custom_total = r_total

    url = 'http://www.customs.go.jp/toukei/srch/index.htm?M=01&P=1,{exp_imp},,,,,,,,1,0,'
    url = url + '{year},0,{month},0,2,{hs9},,,,,,,,,,1,,,,,,,,,,,,,,,,,,,,,,200'
    print(url.format(exp_imp=exp_imp,year=year,month=month,hs9=hs9))
    driver = webdriver.PhantomJS()
    driver.implicitly_wait(5) 
    driver.get(url.format(exp_imp=exp_imp,year=year,month=month,hs9=hs9))
    time.sleep(3)
    driver.switch_to_frame('FR_M_INFO')
    tables = BeautifulSoup(driver.page_source,'lxml').find_all('table')
    total = int(tables[2].find_all('tr')[2].find_all('td')[5].text)
    return(custom_total,total)
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2