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

get_userdcar.py

TDCustmerSupportJP Jul 30th, 2017 (edited) 74 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 = "used_car.csv"
  24. writecsv = csv.writer(file(filename, 'w'), lineterminator='\n')
  25.  
  26. def get_items(jsn):
  27.         dic = {}
  28.         dic['id']         = jsn['id']
  29.         dic['model']      = jsn['model']
  30.         dic['grade']      = jsn['grade']
  31.         dic['color']      = jsn['color']
  32.         dic['recycle']    = jsn['recycle']
  33.         dic['engine']     = jsn['engine']
  34.         dic['price']      = jsn['price']
  35.         dic['inspection'] = jsn['inspection']
  36.         dic['year']       = jsn['year']
  37.         dic['desc']       = jsn['desc']
  38.  
  39.         dic['odd']        = jsn['odd'].encode('utf-8')
  40.         m = re.match('(.*)万km' ,dic['odd'])
  41.         if m:
  42.             dic['odd_km'] = int(float(m.group(1))*10000)
  43.         else:
  44.             dic['odd_km'] = ''
  45.  
  46.         brand_jsn         = jsn['brand']
  47.         dic['name']       = brand_jsn['name']
  48.         dic['code']       = brand_jsn['code']
  49.  
  50.         body_jsn  = jsn['body']
  51.         dic['body_name'] = body_jsn['name']
  52.         dic['body_code'] = body_jsn['code']
  53.  
  54.         shop_jsn              = jsn['shop']
  55.         dic['shop_lat']       = shop_jsn ['lat']
  56.         dic['shop_lng']       = shop_jsn ['lng']
  57.         dic['shop_name']      = shop_jsn['name']
  58.         dic['shop_datum']     = shop_jsn['datum']
  59.  
  60.         shop_pref_jsn         = shop_jsn['pref']
  61.         dic['shop_pref_name'] = shop_pref_jsn['name']
  62.         dic['shop_pref_code'] = shop_pref_jsn['code']
  63.  
  64.         urls_jsn   = jsn['urls']
  65.         dic['url_qr']     = urls_jsn['qr']
  66.         dic['url_mobile'] = urls_jsn['mobile']
  67.         dic['url_pc']     = urls_jsn['pc']
  68.  
  69.         dic['maintenance_comment'] = jsn['maintenance_comment']
  70.         dic['maintenance']         = jsn['maintenance']
  71.         dic['maintenance_fee']     = jsn['maintenance_fee']
  72.  
  73.         dic['warranty']          = jsn['warranty']
  74.         dic['warranty_fee']      = jsn['warranty_fee']
  75.         dic['warranty_comment']  = jsn['warranty_comment']
  76.  
  77.         dic['warranty_length']   = jsn['warranty_length'].encode('utf-8')
  78.         m = re.match('保証期間:(.*)', dic['warranty_length'])
  79.         if m:
  80.             dic['warranty_length_kikan'] = m.group(1)
  81.         else:
  82.             dic['warranty_length_kikan'] = ''
  83.         m = re.match('保証期限:(.*)', dic['warranty_length'])
  84.         if m:
  85.             dic['warranty_length_kigen'] = m.group(1)
  86.         else:
  87.             dic['warranty_length_kigen'] = ''
  88.  
  89.         dic['warranty_distance'] = jsn['warranty_distance'].encode('utf-8')
  90.         m = re.match('保証距離:(.*)', dic['warranty_distance'])
  91.         if m:
  92.             s = m.group(1)
  93.             mm = re.match('(\d*)km',s)
  94.             if mm:
  95.                 dic['warranty_distance'] = mm.group(1)
  96.             else:
  97.                 dic['warranty_distance'] = s
  98.  
  99.         photo_jsn           = jsn['photo']
  100.         photo_main_jsn      = photo_jsn['main']
  101.         dic['main_caption'] = photo_main_jsn['caption']
  102.         dic['main_photo_l'] = photo_main_jsn['l']
  103.         dic['main_photo_s'] = photo_main_jsn['s']
  104.  
  105.         return dic
  106.  
  107. ##### main #####
  108. for country in countries:
  109.     print country
  110.     contents = [0]
  111.     start = 1
  112.     header_flag = True
  113.     counter = 0
  114.     # url info
  115.     while 0 < len(contents):
  116.         base_url = "http://webservice.recruit.co.jp/carsensor/usedcar/v1/?key=%s&country=%s&format=json&count=%s&start=%s"%(api_key, country, count, start)
  117.         print base_url
  118.         res =  json.loads(get_result(base_url))
  119.         contents = res["results"]["usedcar"]
  120.         if len(contents)==0: continue
  121.         for jsn in contents:
  122.             dic = get_items(jsn)
  123.             keys = sorted(dic.keys())
  124.             if header_flag:
  125.               writecsv.writerow(keys)
  126.               header_flag = False
  127.             values = []
  128.             for k in sorted(dic.keys()):
  129.                 k = k.encode('utf-8')
  130.                 if  isinstance(dic[k],unicode):
  131.                     v = dic[k].encode('utf-8')
  132.                 else:
  133.                     v = dic[k]
  134.                 values.append(v)
  135.             writecsv.writerow(values)
  136.             counter += 1
  137.         start += 100
  138.         print counter
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