Created
July 21, 2015 22:03
-
-
Save jinwood/e9610ee113e0fc9352d5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import csv | |
import os | |
import sys | |
from lxml import etree | |
try: | |
postcode = sys.argv[1] | |
except: | |
print "you need to enter a postcode silly!" | |
sys.exit() | |
payload = {'api_key': 'your_api_key_here', | |
'postcode': postcode, | |
'listing_status': 'rent', | |
'maximum_beds': '3', | |
'minimum_price': '100', | |
'maximum_price': '400', | |
'page_size': '100'} | |
print "postcode - %s" % postcode | |
print "fetching data from zoopla" | |
r = requests.get("http://api.zoopla.co.uk/api/v1/property_listings.xml", params = payload) | |
r.ecoding = 'utf-8' | |
response = r.content | |
print "done" | |
tree = etree.ElementTree(etree.fromstring(response)) | |
tree.write("zoopla_data.xml") | |
out = "result.csv" | |
out_data = [] | |
parser = etree.XMLParser() | |
file_name = "zoopla_data.xml" | |
root = etree.parse(file_name, parser) | |
tag_list = [ | |
"property_type", | |
"outcode", | |
"rental_prices", | |
"num_bathrooms", | |
"num_bedrooms", | |
"num_floors", | |
"num_recepts", | |
"details_url"] | |
out_data.append(tag_list[:]) | |
def get_tags(n): | |
info = [] | |
for tag in tag_list: | |
if tag is "rental_prices": | |
node = n.find("rental_prices/per_month") | |
else: | |
node = n.find(tag) | |
if node is not None and node.text: | |
info.append(node.text) | |
else: | |
info.append("") | |
return info | |
print "reading xml" | |
data = root.findall("//listing") | |
for d in data: | |
res = get_tags(d) | |
if res: | |
out_data.append(res) | |
print "finished xml, writing file" | |
out_file = open(out, "wb") | |
csv_writer = csv.writer(out_file, quoting=csv.QUOTE_MINIMAL) | |
for row in out_data: | |
try: | |
csv_writer.writerow(row) | |
except: | |
print "error - %s" % row | |
out_file.close() | |
os.remove("zoopla_data.xml") | |
print "bye" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment