-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflat_crawl.py
153 lines (133 loc) · 4.79 KB
/
flat_crawl.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
#!/usr/bin/python3
import urllib.request as ur
import lxml.html as html
import datetime as dtd
import csv
import time
import random
url = 'https://www.immonet.de/nordrhein-westfalen/duisburg-wohnung-mieten.html'
csvFileName = "/home/pi/test.csv"
def http_get_content(url):
req = ur.Request(url,
data = None,
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0'
})
content = ur.urlopen(req).read().decode('utf-8')
return content
def get_address(htmlAddress):
street = htmlAddress[0].strip().replace("\n", '').replace("\t", '')
city = htmlAddress[1].strip().replace("\n", '').replace("\t", '')
print(street)
print(city)
return (street, city)
def get_offer_link(offer):
return 'https://www.immonet.de/'+offer
def check_for_link_occurance(link):
try:
fd = open(csvFileName, "r")
except(FileNotFoundError):
return False
reader = csv.reader(fd)
while True:
try:
fields = next(reader)
except(StopIteration):
return False
if link == fields[-1]:
return True
def write_offer_csv(offer):
if check_for_link_occurance(offer):
return
url = get_offer_link(offer)
content = http_get_content(url)
tree = html.fromstring(content)
date = dtd.date.today()
street, city = get_address(tree.xpath('//p[@class="text-100 pull-left"]/text()'))
try:
title = tree.xpath('//title/text()')[0].strip()
except(IndexError):
title = "N/A"
try:
rooms = tree.xpath('//div[@id="equipmentid_1"]/text()')[0].strip()
except(IndexError):
rooms = "N/A"
try:
space = tree.xpath('//div[@id="areaid_1"]/text()')[0].strip()
except(IndexError):
space = "N/A"
try:
contactName = tree.xpath('//p[@id="bdContactName"]/text()')[0].strip()
except(IndexError):
contactName = "N/A"
try:
contactStreet = tree.xpath('//p[@id="bdContactStreet"]/text()')[0].strip()
except(IndexError):
contactStreet = "N/A"
try:
contactZipCity = tree.xpath('//p[@id="bdContactZipCity"]/text()')[0].strip()
except(IndexError):
contactZipCity = "N/A"
try:
contactPhone = tree.xpath('//p[@id="bdContactPhone"]/text()')[0].strip().replace('\t', '').replace('\n', '').replace('"', '')
except(IndexError):
contactPhone = "N/A"
try:
brokerFirmname = tree.xpath('//span[@id="bdBrokerFirmname"]/text()')[0].strip()
except(IndexError):
brokerFirmname = "N/A"
try:
brokerName = tree.xpath('//p[@id="bdBrokerNam"]/text()')[0].strip()
except(IndexError):
brokerName = "N/A"
try:
brokerStreet = tree.xpath('//p[@id="bdBrokerStreet"]/text()')[0].strip()
except(IndexError):
brokerStreet = "N/A"
try:
brokerZipCity = tree.xpath('//p[@id="bdBrokerZipCity"]/text()')[0].strip()
except(IndexError):
brokerZipCity = "N/A"
try:
brokerPhone = tree.xpath('//p[@id="bdBrokerPhone"]/text()')[0].strip().replace('\t', '').replace('\n', '').replace('"', '')
except(IndexError):
brokerPhone = "N/A"
try:
price = tree.xpath('//div[@id="priceid_2"]/text()')[0].strip()
except(IndexError):
price = "N/A"
try:
incidentals = tree.xpath('//div[@id="priceid_20"]/text()')[0].strip()
except(IndexError):
incidentals = "N/A"
link = offer
try:
fd = open(csvFileName, "r")
fd.close()
except(FileNotFoundError):
fd = open(csvFileName, "w+")
fd.write("date,street,city,rooms,space,contactName,contactStreet,contactZipCity,contactPhone,brokerFirmname,brokerName,brokerStreet,brokerZipCity,brokerPhone,price,incidentals,link\n")
fd.close()
print (title)
csvLine = [date, street, city, rooms, space, contactName, contactStreet, contactZipCity, contactPhone,\
brokerFirmname, brokerName, brokerStreet, brokerZipCity, brokerPhone, price, incidentals, title, link]
with open(csvFileName, "a+") as csvFd:
csvWriter = csv.writer(csvFd)
csvWriter.writerow(csvLine)
time.sleep(random.randint(1, 10))
content = http_get_content(url)
tree = html.fromstring(content)
links = tree.xpath('//div[@class="flex-grow-1 overflow-hidden box-25"]/a/@href')
while True:
nextPage = tree.xpath('//a[@class="col-sm-3 col-xs-1 pull-right text-right"]/@href')
for l in links:
print(l)
write_offer_csv(l)
try:
nextPage = nextPage[0]
except(IndexError):
quit()
nextPage = get_offer_link(nextPage)
content = http_get_content(nextPage)
tree = html.fromstring(content)
links = tree.xpath('//div[@class="flex-grow-1 overflow-hidden box-25"]/a/@href')