-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscrape.py
More file actions
95 lines (78 loc) · 2.58 KB
/
Copy pathscrape.py
File metadata and controls
95 lines (78 loc) · 2.58 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
#!/usr/bin/env python3
import argparse
import sys
from typing import Iterable, Optional
import requests
from bs4 import BeautifulSoup
DEFAULT_USER_AGENT = "invlect-parser/0.1 (+https://example.com)"
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Fetch a web page and optionally extract elements via a CSS selector."
)
parser.add_argument("url", help="URL to fetch")
parser.add_argument(
"-s",
"--selector",
help="CSS selector used to narrow the output to specific elements",
)
parser.add_argument(
"-a",
"--attribute",
help="Optional attribute to read from each matched element",
)
parser.add_argument(
"-m",
"--max-items",
type=int,
help="Limit the number of matching elements returned",
)
parser.add_argument(
"--timeout",
type=float,
default=15.0,
help="HTTP request timeout in seconds (default: 15)",
)
parser.add_argument(
"--user-agent",
default=DEFAULT_USER_AGENT,
help=f"User-Agent header to send (default: {DEFAULT_USER_AGENT})",
)
return parser.parse_args()
def fetch(url: str, timeout: float, user_agent: str) -> str:
headers = {"User-Agent": user_agent}
try:
response = requests.get(url, headers=headers, timeout=timeout)
response.raise_for_status()
except requests.RequestException as exc:
raise SystemExit(f"Request failed: {exc}") from exc
return response.text
def stringify_elements(elements: Iterable, attribute: Optional[str]) -> str:
collected = []
for element in elements:
if attribute:
value = element.get(attribute)
if value is None:
continue
collected.append(value)
else:
collected.append(element.get_text(separator=" ", strip=True))
return "\n".join(collected)
def extract(html: str, selector: Optional[str], attribute: Optional[str], limit: Optional[int]) -> str:
if not selector:
return html
soup = BeautifulSoup(html, "lxml")
matches = soup.select(selector)
if not matches:
return ""
if limit is not None:
matches = matches[:limit]
return stringify_elements(matches, attribute)
def main() -> None:
args = parse_args()
html = fetch(args.url, args.timeout, args.user_agent)
output = extract(html, args.selector, args.attribute, args.max_items)
if not output:
return
sys.stdout.write(output.rstrip("\n") + "\n")
if __name__ == "__main__":
main()