-
Notifications
You must be signed in to change notification settings - Fork 1
/
crawler.py
306 lines (254 loc) · 11.2 KB
/
crawler.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import argparse
import ctypes
import json
import os
import time
import traceback
from datetime import datetime
from pathlib import Path
from random import randint
from typing import Optional
import requests
import urllib3
from bs4 import BeautifulSoup
from libchrome import Chrome
from liblogger import log_err, log_inf
urllib3.disable_warnings()
COOKIE = ""
USER_AGENT = ""
CUR_DIR = str(Path(__file__).parent.absolute())
TEMP_DIR = os.path.join(CUR_DIR, "temp")
OUTPUT_DIR = os.path.join(CUR_DIR, "output")
DONE_MARKER_NAME = "done"
CHROME = None
# Nürnberg
# HOME_URL = "https://www.hwk-mittelfranken.de/betriebe/suche-75,0,bdbsearch.html?search-searchterm=&search-filter-zipcode=90402&search-filter-radius=250&search-filter-jobnr=&search-job=&search-local=&search-filter-training=&search-filter-experience="
# BASE_URL = "https://www.hwk-mittelfranken.de"
# COOKIE_DOMAIN = ".hwk-mittelfranken.de"
# TOTAL_PAGES = 1439
# PAGE_SIZE = 10
# Rostock
# HOME_URL = "https://www.hwk-omv.de/betriebe/suche-18,57,bdbsearch.html?search-searchterm=&search-filter-zipcode=Rostock&search-filter-radius=250&search-filter-jobnr=&search-job=&search-local=&search-filter-training=&search-filter-experience="
# BASE_URL = "https://www.hwk-omv.de"
# COOKIE_DOMAIN = ".hwk-omv.de"
# TOTAL_PAGES = 587
# PAGE_SIZE = 10
# Köln
HOME_URL = "https://www.hwk-koeln.de/betriebe/suche-32,0,bdbsearch.html?search-searchterm=&search-filter-zipcode=50667&search-filter-radius=250&search-filter-jobnr=&search-job=&search-local=&search-filter-experience="
BASE_URL = "https://www.hwk-koeln.de"
COOKIE_DOMAIN = ".hwk-koeln.de"
TOTAL_PAGES = 2716
PAGE_SIZE = 10
def gen_page_url(page_index: int) -> str:
# Nürnberg
# page_link = f"https://www.hwk-mittelfranken.de/betriebe/suche-75,0,bdbsearch.html?search-searchterm=&search-job=&search-local=&search-filter-zipcode=90402&search-filter-latitude=49.453333&search-filter-longitude=11.091667&search-filter-radius=250&search-filter-jobnr=&search-filter-training=&search-filter-experience=&limit={PAGE_SIZE}&offset={page_index * PAGE_SIZE}"
# Rostock
# page_link = f"https://www.hwk-omv.de/betriebe/suche-18,57,bdbsearch.html?limit={PAGE_SIZE}&search-searchterm=&search-job=&search-local=&search-filter-zipcode=Rostock&search-filter-radius=250&search-filter-jobnr=&search-filter-training=&search-filter-experience=&offset={page_index * PAGE_SIZE}"
# Köln
page_link = f"https://www.hwk-koeln.de/betriebe/suche-32,0,bdbsearch.html?limit={PAGE_SIZE}&search-searchterm=&search-job=&search-local=&search-filter-zipcode=50667&search-filter-latitude=50.941389&search-filter-longitude=6.953611&search-filter-radius=250&search-filter-jobnr=&search-filter-experience=&offset={page_index * PAGE_SIZE}"
return page_link
def mark_as_done(dir_path: str):
try:
with open(os.path.join(dir_path, DONE_MARKER_NAME), "w") as f:
f.write("done")
except:
traceback.print_exc()
def is_done(dir_path: str) -> bool:
ret = False
try:
ret = os.path.isfile(os.path.join(dir_path, DONE_MARKER_NAME))
except:
traceback.print_exc()
return ret
def get_cookie() -> Optional[str]:
cookie_header = None
if CHROME != None:
CHROME.clear_cookie()
while not CHROME.goto(
url2go=HOME_URL,
wait_elem_selector=".searchhit-result",
wait_timeout=300.0,
):
pass
while True:
cookies = CHROME.cookie(COOKIE_DOMAIN)
if cookies != None:
cookie_header = ""
for cookie in cookies:
cookie_header += f"{cookie['name']}={cookie['value']}; "
cookie_header = cookie_header.strip().rstrip(";")
if cookie_header == "":
# log_err("Cookie is empty, Retry get cookie")
time.sleep(0.1)
else:
break
else:
log_err("Cookie is none")
else:
log_err("chrome is none")
return cookie_header
def fetch(url: str, delay: float = 1.0) -> Optional[BeautifulSoup]:
global COOKIE
ret = None
try:
if not url.startswith(BASE_URL):
url = BASE_URL + url
while True:
try:
time.sleep(delay)
resp = requests.get(
url,
headers={
"Cookie": COOKIE,
"User-Agent": USER_AGENT,
},
verify=False,
timeout=15.0,
)
if resp.status_code == 200:
if "Just a moment..." in resp.text:
log_err("cloudflare")
else:
ret = BeautifulSoup(resp.text, "html.parser")
break
else:
log_err(f"request error: {resp.status_code}")
log_inf("fetch new cookie")
COOKIE = get_cookie()
log_inf(f"cookie > {COOKIE}")
except:
traceback.print_exc()
except:
traceback.print_exc()
return ret
def crawl_page(page_index: int, page_link: str):
try:
page_dir = os.path.join(OUTPUT_DIR, ("page_%04d" % page_index))
os.makedirs(page_dir, exist_ok=True)
if is_done(page_dir):
log_inf(f"page {page_index} is already done")
else:
log_inf(f"page {page_index} > {page_link}")
# fetch company list
soup = fetch(page_link)
if soup != None:
result_elems = soup.select(".searchhit-result")
for i, result_elem in enumerate(result_elems):
log_inf(f"crawl page {page_index} > info {i}")
info_fpath = os.path.join(page_dir, f"{i}.json")
if os.path.isfile(info_fpath):
log_inf(f"page {page_index} > info {i} is already done")
continue
info = {
"name": "#",
"address": "#",
"email": "#",
"telephone": "#",
"mobile": "#",
"fax": "#",
}
link_elem = result_elem.select_one("a")
if link_elem != None:
info_link = link_elem.attrs["href"]
name = link_elem.text.strip()
info["name"] = name
# fetch info
soup = fetch(info_link)
if soup != None:
cards = soup.select(".container.content>div.row:nth-child(2)>div.col-md-3")
# address
p_elem = cards[0].select_one("p")
if p_elem != None:
content = p_elem.decode_contents()
words = content.split("<br/>")
street = words[1].strip()
zip = words[2].split(" ", 1)[0].strip()
city = words[2].split(" ", 1)[1].strip()
address = f"{city} {street}"
info["address"] = address
else:
log_err("p_elem is none")
if len(cards) > 1:
p_elem = cards[1].select_one("p")
if p_elem != None:
# email
email_elem = p_elem.select_one("a.mail")
if email_elem != None:
email = email_elem.text.strip()
info["email"] = email.replace("--at--", "@")
else:
log_err("email elem is none")
# phone
contact = p_elem.decode_contents()
words = contact.split("<br/>")
for word in words:
word = word.lower().strip()
if word.startswith("telefon"):
info["telephone"] = word.replace("telefon", "").strip()
elif word.startswith("handy"):
info["mobile"] = word.replace("handy", "").strip()
elif word.startswith("fax"):
info["fax"] = word.replace("fax", "").strip()
else:
log_err("p_elem is none")
else:
log_err("no contact")
else:
log_err("failed fetch company content")
else:
log_err("failed get link elem")
tmp_fpath = info_fpath + ".tmp"
with open(tmp_fpath, "w") as f:
json.dump(info, f, indent=2)
os.rename(tmp_fpath, info_fpath)
else:
log_err("failed fetch page content")
mark_as_done(page_dir)
except:
traceback.print_exc()
def work(start: int, count: int):
try:
global CHROME, USER_AGENT
begin_page = start
end_page = min(TOTAL_PAGES, start + count)
if count == 0:
end_page = TOTAL_PAGES
log_inf(f"From {begin_page} page To {end_page} page")
ctypes.windll.kernel32.SetConsoleTitleW(f"From {begin_page} page To {end_page} page")
CHROME = Chrome(
width=800 + randint(0, 200),
height=600 + randint(0, 100),
user_data_dir=os.path.join(TEMP_DIR, f"profile_{datetime.now().timestamp()}"),
)
CHROME.start()
USER_AGENT = CHROME.run_script("navigator.userAgent")
for i in range(begin_page, end_page):
crawl_page(page_index=i, page_link=gen_page_url(i))
CHROME.quit()
log_inf("All done.")
except:
traceback.print_exc()
def main():
global STATE, FROM_CITY, CITY_COUNT
parser = argparse.ArgumentParser()
parser.add_argument(
"--start",
dest="start",
type=int,
default=0,
required=False,
help="Start page index. Default is 0.",
)
parser.add_argument(
"--count",
dest="count",
type=int,
default=0,
required=False,
help="Page count. Default is 0 which means all the following pages.",
)
args = parser.parse_args()
work(start=args.start, count=args.count)
input("Press ENTER to exit.")
if __name__ == "__main__":
main()