Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

get_catalog_master.py

TDCustmerSupportJP Jul 30th, 2017 (edited) 51 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. import sys,os,re
  6. import time,datetime
  7. import json,msgpack,yaml,csv
  8. from pprint import pprint
  9. import urllib
  10. from urllib2 import Request, urlopen, URLError, HTTPError
  11. from pprint import pprint
  12.  
  13. def get_result(url):
  14.     fp = urlopen(url); contents = fp.read(); fp.close();
  15.     return contents
  16.  
  17. # params
  18. api_key = "XXXXXX" # https://webservice.recruit.co.jp/register/index.html よりAPI KEY を取得して下さい。
  19. countries = ["JPN", "GER","USA","CAN","GBR","SWE","FRA","ITA","AUT","ESP","BEL","HOL","RUS","AUS","KOR","THA","MYS","IND","RSA","ZZZ"]
  20. count = 100
  21.  
  22. # csv
  23. filename = "catalog_master.csv"
  24. writecsv = csv.writer(file(filename, 'w'), lineterminator='\n')
  25.  
  26. def get_items(jsn):
  27.         dic = {}
  28.         dic["width"]  = jsn["width"]
  29.         dic["period"] = jsn["period"]
  30.         dic["grade"]  = jsn["grade"]
  31.         dic["model"]  = jsn["model"]
  32.  
  33.         brand_jsn   = jsn['brand']
  34.         dic['name'] = brand_jsn['name']
  35.         dic['code'] = brand_jsn['code']
  36.  
  37.         dic["height"] = jsn["height"]
  38.         dic["series"] = jsn["series"]
  39.  
  40.         body_jsn         = jsn['body']
  41.         dic["body_name"] = body_jsn["name"]
  42.         dic["body_code"] = body_jsn["code"]
  43.  
  44.         dic["length"] = jsn["length"]
  45.         dic["desc"]   = jsn["desc"]
  46.         dic["person"] = jsn["person"]
  47.         dic["price"]  = int(jsn["price"])
  48.         return dic
  49.  
  50. ##### main #####
  51. for country in countries:
  52.     print country
  53.     contents = [0]
  54.     start = 1
  55.     header_flag = True
  56.     # url info
  57.     while 0 < len(contents):
  58.         base_url = "http://webservice.recruit.co.jp/carsensor/catalog/v1/?key=%s&country=%s&format=json&count=%s&start=%s"%(api_key, country, count, start)
  59.         print base_url
  60.         res =  json.loads(get_result(base_url))
  61.         contents = res["results"]["catalog"]
  62.         if len(contents)==0: continue
  63.         for jsn in contents:
  64.             dic = get_items(jsn)
  65.             keys = sorted(dic.keys())
  66.             if header_flag:
  67.               writecsv.writerow(keys)
  68.               header_flag = False
  69.             values = []
  70.             for k in sorted(dic.keys()):
  71.                 # print "%s: %s"%(k,dic[k])
  72.                 k = k.encode('utf-8')
  73.                 if  isinstance(dic[k],unicode):
  74.                     v = dic[k].encode('utf-8')
  75.                 else:
  76.                     v = dic[k]
  77.                 values.append(v)
  78.             writecsv.writerow(values)
  79.         start += 100
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top