forked from parakh-gupta/Web_Scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webScraper.py
159 lines (129 loc) · 4.89 KB
/
webScraper.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
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import os
from bs4 import BeautifulSoup
from selenium import webdriver
import sys
import json
# In[2]:
chromedriver = "C:/Users/Parakh Gupta/Downloads/setups/chromedriver" # path to the chromedriver executable
chromedriver = os.path.expanduser(chromedriver)
print('chromedriver path: {}'.format(chromedriver))
sys.path.append(chromedriver)
driver = webdriver.Chrome(chromedriver)
# In[3]:
def get_house_links( driver):
house_links=[]
cities=['Gurgaon','Noida','Ghaziabad','Greater-Noida','Bangalore','Mumbai','Pune','Hyderabad','Kolkata','Chennai',
'New-Delhi','Ahmedabad','Navi-Mumbai','Thane','Faridabad','Bhubaneswar','Bokaro-Steel-City','Vijayawada','Vrindavan''Bhopal',
'Gorakhpur','Jamshedpur','Agra','Allahabad','Jodhpur''Aurangabad','Jaipur','Mangalore','Nagpur','Guntur','Navsari','Palghar','Salem','Haridwar','Durgapur',
'Madurai','Manipal','Patna','Ranchi','Raipur','Sonipat','Kottayam','Kozhikode','Thrissur','Tirupati','Trivandrum','Trichy','Udaipur','Vapi','Varanasi',
'Vadodara','Visakhapatnam','Surat','Kanpur','Kochi','Mysore','Goa','Bhiwadi','Lucknow','Nashik','Guwahati','Chandigarh','Indore','Coimbatore','Dehradun']
for city in cities:
for i in range(1,90):
driver.get("https://www.magicbricks.com/property-for-sale/residential-real-estate?proptype=Multistorey-Apartment,Builder-Floor-Apartment,Penthouse,Studio-Apartment,Residential-House,Villa&page="+str(i)+"&cityName="+str(city))
soup = BeautifulSoup(driver.page_source, 'html.parser')
get_data(soup)
# In[4]:
def get_data(soup):
h=[]
prop=soup.find_all('div',class_='m-srp-card SRCard')
for i in prop:
name=""
id_=""
description=""
url=""
price=""
priceInWord=""
cityName=""
addressLocality=""
longitude=""
latitude=""
numberOfRooms=""
bathroom=""
bedroom=""
floorSize=""
floorno=""
furnshingstatus=""
agentName=""
agentCompanyName=""
agentMaskedmobilenumber=""
meta=i.find_all('meta')
for m in meta:
if m['itemprop']=='name':
name=m['content']
if m['itemprop']=='description':
description=m['content']
if m['itemprop']=='url':
url=str('https://www.magicbricks.com'+m['content'])
if m['itemprop']=='addressLocality':
addressLocality=m['content']
if m['itemprop']=='longitude':
longitude=m['content']
if m['itemprop']=='latitude':
latitude=m['content']
if m['itemprop']=='numberOfRooms':
numberOfRooms=m['content']
if m['itemprop']=='floorSize':
floorSize=m['content']
s=i.find('span',class_='hidden')
bathroom=s['data-bathroom']
bedroom=s['data-bedroom']
floorno=s['data-floorno']
furnshingstatus=s['data-furnshingstatus']
price=s['data-price']
id_=s['id']
ag=soup.find('span',id=id_)
priceInWord=ag['data-priced']
cityName=ag['data-cityname']
agentName=ag['data-soname']
agentCompanyName=ag['data-companyname']
agentMaskedmobilenumber=ag['data-maskedmobilenumber']
var = {
"name":name,
"id":id_,
"description":description,
"url":url,
"price":price,
"priceInWord":priceInWord,
"location":
{
"cityName":cityName,
"addressLocality":addressLocality,
"longitude":longitude,
"latitude":latitude,
},
"flatDetails":
{
"numberOfRooms":numberOfRooms,
"bathroom":bathroom,
"bedroom":bedroom,
"floorSize":floorSize,
"floorno":floorno,
"furnshingstatus":furnshingstatus,
},
"agentDetails":
{
"agentName":agentName,
"agentCompanyName":agentCompanyName,
"agentMaskedmobilenumber":agentMaskedmobilenumber,
}
}
#print(var)
h.append(var)
with open("data.json",'r+') as f:
feeds = json.load(f)
for i in h:
feeds['property'].append(var)
f.seek(0)
json.dump(feeds, f)
# In[5]:
v={
"property":[]
}
with open("data.json", mode='w', encoding='utf-8') as f:
json.dump(v,f)
# In[ ]:
get_house_links(driver)
# In[ ]: