-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscrap.py
149 lines (132 loc) · 5.09 KB
/
scrap.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
from bs4 import BeautifulSoup
from urllib.request import Request, urlopen
import os
import pandas as pd
import re
import sqlite3
def scrap(link):
try:
req = Request(link, headers={'User-Agent': 'Mozilla/5.0'})
response = urlopen(req)
soup = BeautifulSoup(response, 'lxml')
except:
# print(f'Error while downloading the page {link}.\n')
# print(f'Message: {e}')
return 0
immo_code = 404
price = 0
area = 0
property_type = "Missing"
rooms = 0
zip_code = 404
land_area = 0
garden_area = 0
kitchen = "No"
pool = "No"
furnished = "No"
terrace = "No"
# immoweb_code
try:
code = soup.find('div', {'class': 'classified__information--immoweb-code'})
code_immo = int(code.text.strip().split(' ')[-1])
immo_code = code_immo
except:
pass
#type_property
try:
type_p = soup.find("div",{"class":"classified__header-content"})
type_immo = type_p.find("h1",{"class":"classified__title"}).text.strip().split(' ')[0]
property_type = type_immo.strip()
except:
pass
# #price
try:
price_t = soup.find("span",{"aria-hidden":"true"}).text.strip().replace(",", "")
price_tu = re.findall(r"\d+", price_t.replace("€", ""))[0]
price = price_tu
except:
pass
# #zip-code
try:
zip_co = link.split('/')[8]
zip_code = zip_co
except:
pass
#rooms number
try:
rooms_t = soup.find_all("span", {'class': 'overview__text'})[0].text.strip()
rooms = rooms_t.split(" ")[0]
except:
pass
#List of the remaining elements
try:
tags = soup.select('tr.classified-table__row')
for tag in tags:
temp = tag.select('th.classified-table__header')[0].contents
if len(temp) > 0:
#area
if str(temp[0]).strip() == "Living area":
area = tag.select('td.classified-table__data')[0].contents
if (len(area) > 0):
area = str(area[0]).strip()
elif str(temp[0]).strip() == "Garden surface":
garden = tag.select('td.classified-table__data')[0].contents
if (len(garden) > 0):
garden_area = str(garden[0]).strip()
elif str(temp[0]).strip() == "Terrace":
terra = tag.select('td.classified-table__data')[0].contents
if (len(terra) > 0):
terrace = str(terra[0]).strip()
elif str(temp[0]).strip() == "Kitchen type":
kitchen_t = tag.select('td.classified-table__data')[0].contents
if (len(kitchen_t) > 0):
kitchen = str(kitchen_t[0]).strip()
elif str(temp[0]).strip() == "Furnished":
furni = tag.select('td.classified-table__data')[0].contents
if (len(furni) > 0):
furnished = str(furni[0]).strip()
elif str(temp[0]).strip() == "Surface of the plot":
land_plot = tag.select('td.classified-table__data')[0].contents
if (len(land_plot) > 0):
land_area = str(land_plot[0]).strip()
except:
pass
try:
tags_b = soup.select('tbody.classified-table__body')
for tag_b in tags_b:
temp_b = tag_b.select('th.classified-table__header')[0].contents
if len(temp_b) > 0:
if str(temp_b[0]).strip() == "Swimming pool":
pool_t = tag_b.select('td.classified-table__data')[0].contents
if (len(pool_t) > 0):
pool = str(pool[0]).strip()
except:
pass
result = [immo_code, zip_code, property_type, price, area, land_area, rooms, garden_area,
terrace, kitchen, furnished, pool]
return result
if __name__ == "__main__":
data = []
df = pd.DataFrame(columns=["immo_code","zip_code","property_type","price","area",
"land_area","rooms","garden_area","terrace","kitchen","furnished","pool"])
df.to_csv("Dataset/test.csv")
# create database
connection = sqlite3.connect("Database/davy.db")
cursor = connection.cursor()
#create table immo_davy
cursor.execute('CREATE TABLE IF NOT EXISTS immo(immo_code INTEGER, zip_code INTEGER, property_type TEXT,'
'price INTEGER, area INTEGER, land_area TEXT, rooms INTEGER, garden_area INTEGER, terrace TEXT,'
'kitchen TEXT, furnished TEXT, pool TEXT)')
with open("Dataset/links.txt", "r") as f:
links = [line.strip() for line in f]
count = 1
for link in links:
test = scrap(link)
try:
# insert values into immo_davy table
result = tuple(test)
cursor.execute('''INSERT INTO immo (immo_code, zip_code, property_type,price, area, land_area, rooms, garden_area, terrace,kitchen, furnished, pool) VALUES (?,?,?,?,?,?,?,?,?,?,?,?);''', result)
connection.commit()
except:
pass
connection.close()