Skip to content

Instantly share code, notes, and snippets.

@carlpch
Last active February 24, 2019 18:59
Show Gist options
  • Save carlpch/80c52fff07d53ca0068fd4d95e2ce6d6 to your computer and use it in GitHub Desktop.
Save carlpch/80c52fff07d53ca0068fd4d95e2ce6d6 to your computer and use it in GitHub Desktop.
UN Comtrade API Request Information

(Accessing UN trade data via UNComtrade API)

Date:2016.10.11 Documentation:http://comtrade.un.org/data/doc/api/

首先是API Request Address: http://comtrade.un.org/api//refs/da/view?parameters

(The output is in JavaScript Object Notation JSON)

最棘手的是到底有哪些 parameters 要提供:

  • type = "C" (Commodities) [string]
  • freq = "A" (Annual) [string]
  • r = "Country Code" (origin country) [string]
  • px = "S1" (SITC Revision 1) [string]
  • ps = "YYYY" (time period ) [string]
  • p = "Country Code" (partner area) [string]
  • rg = "1" import, "2" export (trade regime / trade flow) [string]
  • cc = "TOTAL" (commidity classification code)
  • max = 500 (maximum records returned, default = 500) [int]
  • fmt = "json" or "csv"
  • head = "H" Human readable headings or "M" Machine readable headings [string]
import requests
import json

# Dictionary
getgrade= {'type': 'C', 
           'freq': 'A',
           'px': 'S1',
           'cc': 'Total',
           'max': '500',
           'fmt': 'json',
           'head':'M',
           'r':'842',
           'p':'156',
           'ps':'1991',
           'rg':['1','2'],
           'cc':'TOTAL'
           }
r = requests.get('http://comtrade.un.org/api/get', params=getgrade)

Return:

{'dataset': [{'AltQuantity': None,
   'CIFValue': None,
   'FOBValue': None,
   'GrossWeight': None,
   'IsLeaf': 0,
   'NetWeight': None,
   'TradeQuantity': None,
   'TradeValue': 20276390554,
   'aggrLevel': 0,
   'cmdCode': 'TOTAL',
   'cmdDescE': 'All commodities',
   'cstCode': '',
   'cstDesc': '',
   'estCode': 0,
   'motCode': '',
   'motDesc': '',
   'period': 1991,
   'periodDesc': '1991',
   'pfCode': 'S1',
   'pt3ISO': 'CHN',
   'pt3ISO2': '',
   'ptCode': 156,
   'ptCode2': None,
   'ptTitle': 'China',
   'ptTitle2': '',
   'qtAltCode': None,
   'qtAltDesc': '',
   'qtCode': 1,
   'qtDesc': 'No Quantity',
   'rgCode': 1,
   'rgDesc': 'Import',
   'rt3ISO': 'USA',
   'rtCode': 842,
   'rtTitle': 'USA',
   'yr': 1991},
  {'AltQuantity': None,
   'CIFValue': None,
   'FOBValue': None,
   'GrossWeight': None,
   'IsLeaf': 0,
   'NetWeight': None,
   'TradeQuantity': None,
   'TradeValue': 6278069753,
   'aggrLevel': 0,
   'cmdCode': 'TOTAL',
   'cmdDescE': 'All commodities',
   'cstCode': '',
   'cstDesc': '',
   'estCode': 0,
   'motCode': '',
   'motDesc': '',
   'period': 1991,
   'periodDesc': '1991',
   'pfCode': 'S1',
   'pt3ISO': 'CHN',
   'pt3ISO2': '',
   'ptCode': 156,
   'ptCode2': None,
   'ptTitle': 'China',
   'ptTitle2': '',
   'qtAltCode': None,
   'qtAltDesc': '',
   'qtCode': 1,
   'qtDesc': 'No Quantity',
   'rgCode': 2,
   'rgDesc': 'Export',
   'rt3ISO': 'USA',
   'rtCode': 842,
   'rtTitle': 'USA',
   'yr': 1991}],
 'validation': {'count': {'durationSeconds': 2.2465295999999997,
   'finished': '2016-10-11T23:14:19.3307725-04:00',
   'started': '2016-10-11T23:14:17.0842429-04:00',
   'value': 2},
  'datasetTimer': {'durationSeconds': 2.5429467,
   'finished': '2016-10-11T23:14:19.6271896-04:00',
   'started': '2016-10-11T23:14:17.0842429-04:00'},
  'message': None,
  'status': {'category': 0,
   'description': '',
   'helpUrl': 'For more reference visit http://comtrade.un.org/data/doc/api/',
   'name': 'Ok',
   'value': 0}}}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment