-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrapeProperty.py
More file actions
164 lines (120 loc) · 5.5 KB
/
ScrapeProperty.py
File metadata and controls
164 lines (120 loc) · 5.5 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import os
import pandas as pd
import numpy as np
import requests
from bs4 import BeautifulSoup
import datetime
# from plotly.graph_objs import *
# import plotly
# from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
class ScrapeProperty:
""" Class for all Google Location Services functions and properties"""
REQ_HEADERS = {'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'accept-encoding': 'gzip, deflate, br',
'accept-language': 'en-US,en;q=0.8',
'upgrade-insecure-requests': '1',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3163.100 Safari/537.36'
}
column_titles = ['Price',
'Area',
'Price Per Area',
'Beds',
'Baths',
'Street',
'City',
'State',
'Broker',
'Month',
'Day',
'Year']
def __init__(self, prev_data_path=None):
self.card_list = []
self.data_new = None
self.data = None
self.data_old = None
if prev_data_path is not None:
self.ReadPrevData(prev_data_path=prev_data_path)
def ScrapeZillow(self, url=None, pages=1):
for page in np.arange(1, pages + 1):
## Getting City Names from the Dictionary
url = url + '/' + str(page) + '_p'
## Check Site
with requests.Session() as zillow_session:
html = zillow_session.get(url, headers=self.REQ_HEADERS)
## Scrape HTML Data
soup = BeautifulSoup(html.content, 'lxml')
self.soup_text = soup.get_text()
## Iterate through Sections of the HTML
cards = soup.find_all('div', {'class': 'zsg-photo-card-caption'})
self.card_list = self.card_list + cards
def ReadSoup(self):
for card_index, card in enumerate(self.card_list):
try:
location_dict = {}
location_dict['Price'] = self.__GetPrice(card=card)
location_dict['Street'], location_dict['City'], location_dict['State'] = self.__GetAddress(card=card)
location_dict['Broker'] = self.__GetBroker(card=card)
location_dict['Beds'], location_dict['Baths'], location_dict['Area'] = self.__GetInfo(card=card)
location_dict['Year'], location_dict['Month'], location_dict['Day'] = self.__GetDate()
location_dict['Price Per Area'] = location_dict['Price'] / location_dict['Area']
self.AddData(location_dict)
except:
pass
def AddData(self, location_dict={}):
for key, value in location_dict.items():
location_dict[key] = [value]
new_data = pd.DataFrame.from_dict(location_dict)
self.data_new = pd.concat([self.data_new, new_data]) if self.data_new is not None else new_data
self.data_new = self.data_new[self.column_titles]
def ReadPrevData(self, prev_data_path=None):
self.data_old = pd.read_csv(prev_data_path, sep=',')
self.data_old = self.data_old.drop(list(self.data_old)[0], axis=1)
self.data_old = self.data_old[self.column_titles]
def CombinePrevData(self):
if self.data_new is not None and self.data_old is not None:
self.data = pd.concat([self.data_old, self.data_new])
print('Combined Old and New Data')
self.data = self.data.drop_duplicates(subset=['Street', 'City', 'Month', 'Day', 'Year'])
else:
print('Data was not combined because either new or old data is empty')
def SaveData(self, data_path=None):
index = data_path.find('.csv')
new_data_path = data_path[:index] + '_new' + data_path[index:]
old_data_path = data_path[:index] + '_old' + data_path[index:]
self.data_old.to_csv(old_data_path, sep=',')
self.data.to_csv(data_path, sep=',')
self.data_new.to_csv(new_data_path, sep=',')
def __GetPrice(self, card=None):
price = card.find('span', {'class': 'zsg-photo-card-price'}).text
price = price.replace(',', '').replace('$', '')
price = ''.join(ch for ch in price if ch.isdigit())
price = float(price)
return price
def __GetAddress(self, card=None):
address = card.find('span', {'class': 'zsg-photo-card-address'}).text
address = address.split(',')
street = address[0]
city = address[1][1:]
state = address[2].replace(' ', '')
return street, city, state
def __GetBroker(self, card=None):
try: broker = card.find('span', {'class': 'zsg-photo-card-broker-name'}).text
except: broker = 'NaN'
return broker
def __GetInfo(self, card=None):
info = card.find('span', {'class': 'zsg-photo-card-info'}).text
info = info.split()
try: beds = float(info[0])
except: beds = 0
try: baths = float(info[3])
except: baths = 0
try:
area = info[6].replace(',', '')
area = ''.join(ch for ch in area if ch.isdigit()) # Remove Letters
area = float(area)
except:
area = 0
return beds, baths, area
def __GetDate(self):
now = datetime.datetime.now()
return now.year, now.month, now.day