-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.py
More file actions
447 lines (376 loc) · 14.2 KB
/
Copy pathscrape.py
File metadata and controls
447 lines (376 loc) · 14.2 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#!/usr/bin/env python3
import argparse
import json
import re
import sys
from dataclasses import dataclass
from datetime import datetime, timezone
from io import BytesIO
from typing import Any
import requests
from pypdf import PdfReader
TARGET_URL = "https://www.vermittlerregister.info/recherche?a=pdf®isternummer={regnum}"
REQUEST_TIMEOUT = 30
class ScrapeError(Exception):
pass
class CaptchaBlocked(ScrapeError):
pass
@dataclass
class FetchResult:
source: str
payload_type: str
data: bytes | str
status_code: int | None = None
content_type: str | None = None
def is_pdf_content(content_type: str | None, content: bytes) -> bool:
if content_type and "pdf" in content_type.lower():
return True
return content.startswith(b"%PDF")
def decode_html(content: bytes, fallback: str = "utf-8") -> str:
try:
return content.decode(fallback, errors="replace")
except Exception:
return content.decode("latin-1", errors="replace")
def looks_like_captcha(text: str) -> bool:
lowered = text.lower()
markers = (
"friendlycaptcha",
"captcha",
"frc-captcha",
"frc-widget",
"verify you are human",
)
return any(marker in lowered for marker in markers)
def extract_pdf_text(pdf_bytes: bytes) -> str:
reader = PdfReader(BytesIO(pdf_bytes))
pages = []
for page in reader.pages:
pages.append(page.extract_text() or "")
return "\n".join(pages)
def parse_labeled_lines(lines: list[str]) -> dict[str, str]:
labeled: dict[str, str] = {}
for i, line in enumerate(lines):
match = re.match(r"^\s*([^:]{2,80}):\s*(.*?)\s*$", line)
if not match:
continue
key = re.sub(r"\s+", " ", match.group(1)).strip()
value = match.group(2).strip()
if not value and i + 1 < len(lines):
nxt = lines[i + 1].strip()
if nxt and ":" not in nxt:
value = nxt
if key and value:
labeled[key] = value
return labeled
def find_first(lines: list[str], pattern: str) -> str | None:
regex = re.compile(pattern, flags=re.IGNORECASE)
for line in lines:
m = regex.search(line)
if m:
if m.lastindex:
return m.group(1).strip()
return line.strip()
return None
def extract_contact_fields(lines: list[str], labeled: dict[str, str]) -> dict[str, Any]:
def pick(label_candidates: tuple[str, ...], pattern: str | None = None) -> str | None:
for k, v in labeled.items():
if any(c.lower() in k.lower() for c in label_candidates):
return v
if pattern:
return find_first(lines, pattern)
return None
email = pick(("E-Mail", "Email"), r"([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,})")
phone = pick(("Telefon", "Tel"), r"((?:\+?\d[\d\s()/.-]{5,}\d))")
fax = pick(("Fax",), r"(Fax[:\s]+(.+)$)")
website = pick(("Internet", "Webseite", "Homepage"), r"(https?://\S+|www\.\S+)")
return {"email": email, "phone": phone, "fax": fax, "website": website}
def extract_address(lines: list[str], labeled: dict[str, str]) -> dict[str, Any]:
address_value = None
for k, v in labeled.items():
if "anschrift" in k.lower() or "adresse" in k.lower():
address_value = v
break
address = {
"raw": address_value,
"street": None,
"postal_code": None,
"city": None,
"country": None,
}
if not address_value:
return address
pc_city = re.search(r"\b(\d{5})\s+([^\d,;]+)$", address_value)
if pc_city:
address["postal_code"] = pc_city.group(1)
address["city"] = pc_city.group(2).strip()
street = address_value[: pc_city.start()].strip(" ,;")
address["street"] = street or None
else:
address["street"] = address_value
return address
def normalize_broker_data(text: str, registernummer: str) -> dict[str, Any]:
lines = [re.sub(r"\s+", " ", line).strip() for line in text.splitlines()]
lines = [line for line in lines if line]
labeled = parse_labeled_lines(lines)
def get_by_label(*candidates: str) -> str | None:
for c in candidates:
for k, v in labeled.items():
if c.lower() in k.lower():
return v
return None
full_name = get_by_label("Name", "Firma", "Unternehmen")
first_name = get_by_label("Vorname")
last_name = get_by_label("Nachname")
company = get_by_label("Firma", "Unternehmen", "Gesellschaft")
status = get_by_label("Status", "Registerstatus")
authority = get_by_label("Registerbehörde", "IHK")
permission = get_by_label("Erlaubnis")
if not first_name and not last_name and full_name:
parts = full_name.split()
if len(parts) >= 2:
first_name = parts[0]
last_name = " ".join(parts[1:])
regnum_in_text = find_first(
lines,
r"(D-[A-Z0-9]{2,}-[A-Z0-9]{2,}-[A-Z0-9]{2,})",
)
return {
"register_number": regnum_in_text or registernummer,
"status": status,
"broker": {
"full_name": full_name,
"first_name": first_name,
"last_name": last_name,
"company": company,
},
"address": extract_address(lines, labeled),
"contact": extract_contact_fields(lines, labeled),
"permissions": [permission] if permission else [],
"register_authority": authority,
"raw_fields": labeled,
"raw_text": text.strip(),
}
def parse_pdf_result(pdf_bytes: bytes, registernummer: str, source: str) -> dict[str, Any]:
text = extract_pdf_text(pdf_bytes)
if not text.strip():
raise ScrapeError(f"{source}: PDF parsed but contained no text.")
parsed = normalize_broker_data(text, registernummer)
return {
"ok": True,
"source": source,
"payload": "pdf",
"data": parsed,
}
def parse_html_result(html_text: str, registernummer: str, source: str) -> dict[str, Any]:
if looks_like_captcha(html_text):
raise CaptchaBlocked(f"{source}: blocked by captcha")
stripped = re.sub(r"<[^>]+>", " ", html_text)
stripped = re.sub(r"\s+", " ", stripped).strip()
if not stripped:
raise ScrapeError(f"{source}: empty HTML payload.")
parsed = normalize_broker_data(stripped, registernummer)
return {
"ok": True,
"source": source,
"payload": "html",
"data": parsed,
}
def fetch_direct_http(registernummer: str) -> FetchResult:
url = TARGET_URL.format(regnum=registernummer)
resp = requests.get(url, timeout=REQUEST_TIMEOUT, allow_redirects=True)
content_type = resp.headers.get("Content-Type", "")
if is_pdf_content(content_type, resp.content):
return FetchResult(
source="direct_http",
payload_type="pdf",
data=resp.content,
status_code=resp.status_code,
content_type=content_type,
)
html = decode_html(resp.content)
if looks_like_captcha(html):
raise CaptchaBlocked("direct_http: blocked by captcha")
return FetchResult(
source="direct_http",
payload_type="html",
data=html,
status_code=resp.status_code,
content_type=content_type,
)
def fetch_with_scrapling(registernummer: str) -> FetchResult:
try:
import scrapling
except Exception as exc:
raise ScrapeError(f"scrapling import failed: {exc}") from exc
url = TARGET_URL.format(regnum=registernummer)
response_obj = None
last_exc: Exception | None = None
for fetcher_name in ("StealthyFetcher", "DynamicFetcher", "Fetcher"):
fetcher = getattr(scrapling, fetcher_name, None)
if fetcher is None:
continue
try:
if fetcher_name in ("StealthyFetcher", "DynamicFetcher"):
response_obj = fetcher.fetch(url, headless=True, network_idle=True, timeout=45000)
else:
response_obj = fetcher.get(url, timeout=45)
break
except Exception as exc:
last_exc = exc
if response_obj is None:
raise ScrapeError(f"scrapling fetch failed: {last_exc}")
body = getattr(response_obj, "body", b"")
headers = dict(getattr(response_obj, "headers", {}) or {})
content_type = headers.get("Content-Type") or headers.get("content-type", "")
status_code = getattr(response_obj, "status", None)
if isinstance(body, str):
body = body.encode("utf-8", errors="replace")
if is_pdf_content(content_type, body):
return FetchResult(
source="scrapling",
payload_type="pdf",
data=body,
status_code=status_code,
content_type=content_type,
)
html = decode_html(body)
if looks_like_captcha(html):
raise CaptchaBlocked("scrapling: blocked by captcha")
return FetchResult(
source="scrapling",
payload_type="html",
data=html,
status_code=status_code,
content_type=content_type,
)
def fetch_with_playwright(registernummer: str) -> FetchResult:
try:
from playwright.sync_api import sync_playwright
except Exception as exc:
raise ScrapeError(f"playwright import failed: {exc}") from exc
url = TARGET_URL.format(regnum=registernummer)
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
context = browser.new_context()
page = context.new_page()
try:
response = page.goto(url, wait_until="networkidle", timeout=45000)
html = page.content()
if response is not None:
headers = response.headers
content_type = headers.get("content-type", "")
body = response.body()
if is_pdf_content(content_type, body):
return FetchResult(
source="playwright",
payload_type="pdf",
data=body,
status_code=response.status,
content_type=content_type,
)
if looks_like_captcha(html):
raise CaptchaBlocked("playwright: blocked by captcha")
return FetchResult(
source="playwright",
payload_type="html",
data=html,
status_code=response.status if response else None,
content_type=response.headers.get("content-type") if response else None,
)
finally:
context.close()
browser.close()
def parse_result(result: FetchResult, registernummer: str) -> dict[str, Any]:
if result.payload_type == "pdf":
if not isinstance(result.data, (bytes, bytearray)):
raise ScrapeError(f"{result.source}: expected bytes for PDF data.")
return parse_pdf_result(bytes(result.data), registernummer, result.source)
if not isinstance(result.data, str):
raise ScrapeError(f"{result.source}: expected string for HTML data.")
return parse_html_result(result.data, registernummer, result.source)
def build_final_output(
registernummer: str,
attempts: list[dict[str, Any]],
success_payload: dict[str, Any] | None,
error: str | None = None,
) -> dict[str, Any]:
return {
"ok": success_payload is not None,
"registernummer": registernummer,
"timestamp_utc": datetime.now(timezone.utc).isoformat(),
"attempts": attempts,
"result": success_payload,
"error": error,
}
def run(registernummer: str) -> dict[str, Any]:
attempts: list[dict[str, Any]] = []
# 1) Direct HTTP
try:
direct = fetch_direct_http(registernummer)
parsed = parse_result(direct, registernummer)
attempts.append(
{
"source": "direct_http",
"ok": True,
"status_code": direct.status_code,
"content_type": direct.content_type,
}
)
return build_final_output(registernummer, attempts, parsed)
except Exception as exc:
attempts.append({"source": "direct_http", "ok": False, "error": str(exc)})
# 2) Scrapling
try:
scrapling_result = fetch_with_scrapling(registernummer)
parsed = parse_result(scrapling_result, registernummer)
attempts.append(
{
"source": "scrapling",
"ok": True,
"status_code": scrapling_result.status_code,
"content_type": scrapling_result.content_type,
}
)
return build_final_output(registernummer, attempts, parsed)
except Exception as exc:
attempts.append({"source": "scrapling", "ok": False, "error": str(exc)})
# 3) Playwright
try:
playwright_result = fetch_with_playwright(registernummer)
parsed = parse_result(playwright_result, registernummer)
attempts.append(
{
"source": "playwright",
"ok": True,
"status_code": playwright_result.status_code,
"content_type": playwright_result.content_type,
}
)
return build_final_output(registernummer, attempts, parsed)
except Exception as exc:
attempts.append({"source": "playwright", "ok": False, "error": str(exc)})
return build_final_output(
registernummer=registernummer,
attempts=attempts,
success_payload=None,
error="All scraping strategies failed.",
)
def main() -> int:
parser = argparse.ArgumentParser(description="Scrape broker details from vermittlerregister.info")
parser.add_argument("registernummer", help="IHK register number, e.g. D-21RP-R1O5O-37")
args = parser.parse_args()
try:
output = run(args.registernummer)
except Exception as exc:
output = {
"ok": False,
"registernummer": args.registernummer,
"timestamp_utc": datetime.now(timezone.utc).isoformat(),
"attempts": [],
"result": None,
"error": f"Fatal error: {exc}",
}
print(json.dumps(output, ensure_ascii=False, indent=2))
return 0 if output.get("ok") else 1
if __name__ == "__main__":
sys.exit(main())