-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblt.py
More file actions
201 lines (153 loc) · 6.59 KB
/
blt.py
File metadata and controls
201 lines (153 loc) · 6.59 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from bs4 import BeautifulSoup
import requests
import pandas as pd
import datetime
import os
# os.chdir('C:\Jovian\Python') update directory path if your running this on your local machine
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import date
import csv
import re
# TODO: add a list of locations and scan through all of them all at Once
locations = '5E550'
base_url = 'https://www.rightmove.co.uk/property-for-sale/find.html?locationIdentifier=REGION%' + location + '&sortType=1&index='
properties_per_page = 24
num_properties = 900
csv_filename = 'filename_temp.csv'
csv_location = location + '_data.csv'
key_phrase_list = ["return", "pcm", "investors"
"rental", "income", "investment", "buy-to-let", "buy to let",
"tenanted"]
leasehold_charges =["service charge", "Service charge", "Service Charge",
"ground rent", "Ground rent", "Ground Rent"]
# Collect & convert HTML data as text , collects multiple pages in form of pagdict list
def page_content(base_url,num_properties,properties_per_page):
i=00
listf=[]
pagedict=[]
while i < num_properties:
i += properties_per_page
listf.append(str(i))
page_idx = listf
for item in page_idx:
page = requests.get(base_url+(item))
if page.status_code !=200:
raise Exception(f"Unable to download {base_url+(item)}")
page_content = page.text
pagedict.append(page_content)
return pagedict
def det_page_content(base_url,num_properties,properties_per_page):
i=00
listf=[]
pagedict=[]
for i in range(num_properties):
page = requests.get(base_url+"#/?channel=RES_BUY")
if page.status_code !=200:
raise Exception(f"Unable to download {base_url}")
page_content = page.text
pagedict.append(page_content)
return page_content
def html_parse(pagedict):
page_doc=[0]*len(pagedict)
for i in range(len(pagedict)):
page_doc[i] = BeautifulSoup(pagedict[i], features="html.parser")
return page_doc
def data_in_text(pagedict,page_doc):
page_links1 = []
price_tag=[0]*len(pagedict)
date_tag=[0]*len(pagedict)
address_tag=[0]*len(pagedict)
bed_tag=[0]*len(pagedict)
number_tag=[0]*len(pagedict)
agent_tag=[0]*len(pagedict)
id_tag=[0]*len(pagedict)
yield_info=[0]*len(pagedict)
for i in range(len(pagedict)):
price_tag[i]=page_doc[i].find_all('div', class_="propertyCard-priceValue")
date_tag[i]=page_doc[i].find_all('span',class_="propertyCard-branchSummary-addedOrReduced")
address_tag[i]=page_doc[i].find_all('address',class_="propertyCard-address")
bed_tag[i]=page_doc[i].find_all('h2',class_="propertyCard-title")
number_tag[i]=page_doc[i].find_all('a',class_="propertyCard-contactsPhoneNumber")
agent_tag[i]=page_doc[i].find_all('img',class_="propertyCard-branchLogo-image")
id_tag[i]=page_doc[i].find_all('a',class_="propertyCard-anchor")
for i in range(len(pagedict)):
for j in range(0,25):
try:
page_links1.append({'Data_Date': date.today().strftime("%d/%m/%Y"),'Property_Published':date_tag[i][j].text.strip().replace(",","-"),'Price':price_tag[i][j].text.strip().replace(",",""),'Address':address_tag[i][j].text.strip().replace(",","-").replace("\n",""),'No_of_Beds':bed_tag[i][j].text.strip().replace(",","-"),'Agent_Name':agent_tag[i][j]['alt'].strip().replace(",","-").replace(" Logo",""),'Contact_Number':number_tag[i][j].text.strip(),'Property_link':'https://www.rightmove.co.uk/properties/'+id_tag[i][j]['id'].replace("prop","")})
except IndexError:
pass
continue
return(page_links1)
def det_data_in_text(pagedict,page_doc):
page_links1 = []
description=[0]*len(pagedict)
data_present=[0]*len(pagedict)
charges_present=[0]*len(pagedict)
for i in range(len(pagedict)):
description[i]=page_doc[i].find_all('div',class_=re.compile("STw8udCxUaBUMfOOZu0iL"))
desc = str(description[i])
for word in key_phrase_list:
if desc.find(word) != -1:
data_present[i] = 1
for word in leasehold_charges:
if desc.find(word) != -1:
charges_present[i] = 1
# make a dataframe out of description and data_present and return it
data = {'Description': description,
'Key_Words_Present': data_present,
'Charges_Present': charges_present}
df = pd.DataFrame(data)
return(df)
def find_yield_data(df):
num_props = len(df)
details_pagedict=[]
for i in range(num_props):
url_to_search = df['Property_link'].iloc[i]
details_pagedict.append(det_page_content(url_to_search,1,1))
det_page_doc = html_parse(details_pagedict)
data_present_df = det_data_in_text(details_pagedict,det_page_doc)
df = df.reset_index(drop=True)
df = pd.concat([df, data_present_df.reindex(df.index)], axis=1)
df=df[df['Key_Words_Present'] != 0]
# df=df[df['Charges_Present'] != 0]
df.to_csv(csv_location)
return df
def write_csv(items, path):
with open(path,'w', encoding="utf-8") as csvfile:
writer = csv.writer(csvfile)
# Write the headers in the first line
headers = list(items[0].keys())
writer.writerow(headers)
# Write one item per line
for item in items:
values = []
for header in headers:
values.append(str(item.get(header, "")))
writer.writerow(values)
def test_run():
pagedict = page_content(base_url,num_properties,properties_per_page)
page_doc = html_parse(pagedict)
data_text = data_in_text(pagedict,page_doc)
csv_file = write_csv(data_text,csv_filename)
df = pd.read_csv(csv_filename)
df['Price'] = df['Price'].str.replace('£','')
df['Price'] = df['Price'].str.replace('NaN','')
df['Price'] = df['Price'].str.replace('POA','')
df['Price'] = df['Price'].str.replace(' ','')
df['Price'] = df['Price'].str.replace('nan','')
df['Price'] = df['Price'].str.replace('ComingSoon','')
new_df=df[df['Price'] != '']
new_df['Price']=new_df['Price'].astype(int)
new_df=new_df[new_df['Price'] < 100000]
yield_df = find_yield_data(new_df)
Total_properties = yield_df['Price'].count()
print(f"Total {Total_properties} properties are currently available for sale.")
# histogram presentation
plt.hist(yield_df['Price'],alpha=0.5,bins=10)
plt.title("Histogram of Property Price")
plt.xlabel("Price")
plt.ylabel("Frequency")
plt.show()
if __name__ == "__main__":
test_run()