Skip to content

Instantly share code, notes, and snippets.

@sohwendy
Created September 25, 2018 03:15
Show Gist options
  • Save sohwendy/f676c57986340d763f99c71db338aeb6 to your computer and use it in GitHub Desktop.
Save sohwendy/f676c57986340d763f99c71db338aeb6 to your computer and use it in GitHub Desktop.
#
import json
import csv
import requests
from pprint import pprint
# curl 'https://www.99.co/api/v1/web/clusters/deXkoa5VewougnTrnKj8nCHb/transactions/table/history?transaction_type=sale&page_num=1&page_size=5' > page0.json
filename = 'page4'
inFile = filename + '.json'
outFile = filename + '.csv'
# Retrieve from file
with open(inFile) as data_file:
response = json.loads(data_file.read())
cheaders = ['Date', 'Block', 'Unit', 'Area sqm', 'Area psf', 'Price', 'Price psf']
rows = response['data']['rows']
# read rows into hash
crows = [None]*len(rows)
for i in range(len(rows)):
row = {}
row['Date'] = rows[i][0]['title']
row['Block'] = rows[i][1]['title']
row['Unit'] = rows[i][2]['title']
row['Area'] = rows[i][3]['subtitle'][:-4]
row['Area psf'] = rows[i][3]['title'][:-5]
row['Price'] = rows[i][4]['title'][2:]
row['Price psf'] = rows[i][4]['subtitle'][3:-5]
crows[i] = row
# write to file
outputFile = open(outFile, 'w')
outputWriter = csv.writer(outputFile)
outputWriter.writerow(cheaders)
for i in range(len(crows)):
r = crows[i]
outputWriter.writerow([r['Date'], r['Block'], r['Unit'], r['Area'], r['Area psf'], r['Price'], r['Price psf']])
outputFile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment