-
Notifications
You must be signed in to change notification settings - Fork 0
/
homevalue.py
executable file
·85 lines (62 loc) · 2.18 KB
/
homevalue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
#
# 09/23/2015
#
# A python application that returns the zillow and trulia home value estimate of a given property
#
# A zillow and trulia api key are required to be present in homevalue.ini file
#
import os
import configparser
import urllib.request
import requests
import xml.etree.ElementTree as ET
from lxml import html
zillow_service = 'http://www.zillow.com/webservice/GetZestimate.htm'
trulia_service = 'http://api.trulia.com/webservices.php'
config = configparser.ConfigParser()
# Either of the following read methods work
#config.read_file(open('homevalue.ini'))
config.read('homevalue.ini')
def get_zillow_estimate_iter_test():
key = config.get('zillow', 'api_key')
houseCode = config.get('zillow', 'property_id')
request = urllib.request.urlopen(zillow_service + "?zws-id=" + key + "&zpid=" + houseCode)
data = request.read()
f = open('housedata.xml', 'wb')
f.write(data)
f.close()
#ET.parse() creates an ElementTree
tree1 = ET.parse('housedata.xml')
root = tree1.getroot()
#ET.fromstring() creats an Element
#this element is equivalent to the root of the above ElementTree
tree2 = ET.fromstring(data)
print("Tree1: " + str(type(tree1)))
print("Tree2: " + str(type(tree2)))
print()
print(ET.tostring(root))
for element in root:
print(element.tag)
for child in element:
print(child.tag, child.attrib, child.text)
print()
print("now the iter")
for value in root.iter('amount'):
print(value.text)
def get_zillow_estimate():
key = config.get('zillow', 'api_key')
houseCode = config.get('zillow', 'property_id')
request = urllib.request.urlopen(zillow_service + "?zws-id=" + key + "&zpid=" + houseCode)
data = request.read()
root = ET.fromstring(data)
for amount in root.iter('amount'):
print("Zillow: " + str(amount.text))
def get_trulia_estimate():
url = config.get('trulia', 'home_url')
page = requests.get(url)
tree = html.fromstring(page.content)
value = tree.xpath('//span[@itemprop="price"]/text()')
print("Trulia: "+ str(value[0]))
get_zillow_estimate()
get_trulia_estimate()