-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWebsiteParser.py
More file actions
179 lines (165 loc) · 6.43 KB
/
Copy pathWebsiteParser.py
File metadata and controls
179 lines (165 loc) · 6.43 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
# -*- coding: utf-8 -*-
__author__ = 'Johannes Gontrum <gontrum@vogelschwarm.com>'
import urllib
import time
import sys
import re
from bs4 import BeautifulSoup
def parse(site, url):
try:
if site == 'eBay':
return __eBay(url)
elif site == 'WG1Zimmer':
return __WG1Zimmer(url)
elif site == 'WGWohnung':
return __WGWohnung(url)
elif site == 'WohnungsBoerse':
return __WohnungsBoerse(url)
elif site == 'Immowelt':
return __Immowelt(url)
elif site == 'ImmoScout24':
return __ImmoScout24(url)
elif site == 'Immonet':
return __Immonet(url)
else:
print "Called with unknown website (" + str(site) + "). Leaving now."
sys.exit(1)
except Exception, e:
print "Failed to catch data for " + str(site) + " with URL " + str(url)
def __eBay(url):
page = urllib.urlopen(url)
ebay_page = BeautifulSoup(page.read())
## Most recent offer
most_recent_ad = ebay_page.find("li", attrs={"class": "ad-listitem "})
## Title
title = most_recent_ad.find("a", attrs={"class": "ad-title"})
## URL
link = "http://kleinanzeigen.ebay.de" + title.get('href')
## Text
text = title.get_text()
## Rent
rent = most_recent_ad.find("strong").get_text()
## Location
location = most_recent_ad.find("h3").get_text(" ")
### Process data
return {"title": text, "url": link, "rent": rent, "location": location, "time": time.strftime("%H:%M:%S")}
def __WG1Zimmer(url):
page = urllib.urlopen(url)
wg_page = BeautifulSoup(page.read())
## Most recent offer
most_recent_ad = wg_page.find("div", attrs={"class": "list-details-ad-wrapper CLR "})
## URL
link = "http://www.wg-gesucht.de/" + most_recent_ad.find("h2").find("a").get("href")
title = most_recent_ad.find("h2").find("a").get_text().strip()
## Rent
rent_raw = most_recent_ad.find("strong", attrs={"class": "list-details-ad-price"})
rentList = rent_raw.find("a").get_text().split()
rent = ""
if len(rentList) == 3:
rent = rentList[2]
else:
rent = " ".join(rentList)
## Location
locationList = most_recent_ad.find("p").get_text().strip().split("\n")
location = ""
if len(locationList) == 2:
location =locationList[0].strip()
else:
location = " ".join(locationList)
### Process data
return {"title": title, "url": link, "rent": rent, "location": location, "time": time.strftime("%H:%M:%S")}
def __WGWohnung(url):
page = urllib.urlopen(url)
wg_page = BeautifulSoup(page.read())
## Most recent offer
most_recent_ad = wg_page.find("div", attrs={"class": "list-details-ad-wrapper CLR "})
## URL
link = "http://www.wg-gesucht.de/" + most_recent_ad.find("h2").find("a").get("href")
title = most_recent_ad.find("h2").find("a").get_text().strip()
## Rent
rent_raw = most_recent_ad.find("strong", attrs={"class": "list-details-ad-price"})
rentList = rent_raw.find("a").get_text().split()
rent = ""
if len(rentList) == 3:
rent = rentList[2]
else:
rent = " ".join(rentList)
## Location
locationList = most_recent_ad.find("p").get_text().strip().split("\n")
location = ""
if len(locationList) == 2:
location = locationList[0].strip()
else:
location = " ".join(locationList)
### Process data
return {"title": title, "url": link, "rent": rent, "location": location, "time": time.strftime("%H:%M:%S")}
def __WohnungsBoerse(url):
# Receiving data from javascript in the html header
page = urllib.urlopen(url)
find_area = re.compile("geocode\(.*?\}\);", re.DOTALL)
offer_raw = find_area.findall(str(page.read()))[0]
id = re.findall(r", \d+,", offer_raw)[0].replace(",","").lstrip()
tokenized = re.findall(r"'.*?'", offer_raw)
# Position | Data
# 0 | Address
# 1 | Rooms
# 2 | Size
# 3 | Rent
# 4,5 | -
# 6 | Title
# 7,8 | ZIP + City
location = tokenized[0].replace("'","")
rent = tokenized[3].replace("'", "").replace("&euro", u"€")
title = tokenized[6].replace("'", "")
link = "http://www.wohnungsboerse.net/immodetail/" + id
return {"title": title, "url": link, "rent": rent, "location": location, "time": time.strftime("%H:%M:%S")}
def __Immowelt(url):
page = urllib.urlopen(url)
immow_page = BeautifulSoup(page.read())
## Most recent offer
most_recent_offer = immow_page.find("div", {"class": "divObject listitem_new_wrap"})
## Title
url = most_recent_offer.find("a")
title = most_recent_offer.find("h3").get_text()
## URL
link = "http://www.immowelt.de" + url.get("href")
## Rent
rent = most_recent_offer.find("div", {"class": "hardfact"}).get_text()
## Location
location_raw = most_recent_offer.find("div", {"class": "location location_exact"}).get_text(" ")
location = ' '.join(location_raw.split())
### Process data
return {"title": title, "url": link, "rent": rent, "location": location, "time": time.strftime("%H:%M:%S")}
def __ImmoScout24(url):
page = urllib.urlopen(url)
immoc_page = BeautifulSoup(page.read())
## Most recent offer
most_recent_offer = immoc_page.find("div", {"class": "resultlist_entry_data"})
## Title
url = most_recent_offer.find("a")
title = url.get("title")
## URL
link = "http://www.immobilienscout24.de" + url.get("href")
## Rent
rent = most_recent_offer.find("dd", {"class": "value"}).get_text().split()[0]
## Location
location = most_recent_offer.find("span", {"class": "street"}).get_text(" ")
### Process data
return {"title": title, "url": link, "rent": rent, "location": location, "time": time.strftime("%H:%M:%S")}
def __Immonet(url):
page = urllib.urlopen(url)
immonet_page = BeautifulSoup(page.read())
## Most recent offer
most_recent_offer = immonet_page.find("div", {"class": "selListItem"})
## Title
url = most_recent_offer.find("a")
title = url.get("title")
## URL
link = "http://www.immonet.de" + url.get("href")
## Rent
rent = most_recent_offer.find("span", {"class": "fsLarge"}).get_text()
## Location
location = most_recent_offer.find("p", {"class": "fsSmall"}).get_text("")
location = " ".join(location.split())
### Process data
return {"title": title, "url": link, "rent": rent, "location": location, "time": time.strftime("%H:%M:%S")}