-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraper.py
More file actions
139 lines (115 loc) · 3.63 KB
/
Copy pathscraper.py
File metadata and controls
139 lines (115 loc) · 3.63 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
# pip install httpx beautifulsoup4 lxml
"""
scraper.py
Given a URL, returns a dict matching the AnalyzeRequest fields.
Returns None for any field it cannot extract — never throws.
"""
import asyncio
import json
import re
import sys
from typing import Any, Dict, List, Optional
import httpx
from bs4 import BeautifulSoup
_USER_AGENT = (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/124.0.0.0 Safari/537.36"
)
_PHONE_RE = re.compile(
r"(?<!\d)"
r"(\+?1[\s.\-]?)?"
r"\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}"
r"(?!\d)"
)
_QR_MARKER = "QR Code Link to This Post"
def extract_phones(text: str) -> List[str]:
"""Return all phone numbers found in text."""
try:
return [m.group().strip() for m in _PHONE_RE.finditer(text)]
except Exception:
return []
def _empty(url: str) -> Dict[str, Any]:
return {
"url": url,
"title": None,
"description": None,
"price_usd": None,
"image_urls": [],
"phones_found": [],
}
async def fetch_listing(url: str) -> Dict[str, Any]:
"""Fetch a Craigslist listing page and return parsed fields."""
result = _empty(url)
try:
async with httpx.AsyncClient(
headers={"User-Agent": _USER_AGENT},
follow_redirects=True,
timeout=10.0,
) as client:
resp = await client.get(url)
resp.raise_for_status()
html = resp.text
except httpx.TimeoutException:
return result
except Exception:
return result
try:
soup = BeautifulSoup(html, "lxml")
# Title
try:
tag = soup.select_one("#titletextonly")
if tag:
result["title"] = tag.get_text(strip=True)
except Exception:
pass
# Price
try:
tag = soup.select_one(".price")
if tag:
raw = tag.get_text(strip=True)
result["price_usd"] = float(raw.replace("$", "").replace(",", "").strip())
except Exception:
pass
# Description — remove QR code div from DOM before extracting text
try:
tag = soup.select_one("#postingbody")
if tag:
for qr in tag.select(".print-qrcode-container, .print-information"):
qr.decompose()
result["description"] = tag.get_text("\n", strip=True).strip() or None
except Exception:
pass
# Image URLs
try:
result["image_urls"] = [
a["href"] for a in soup.select("#thumbs a") if a.get("href")
]
except Exception:
result["image_urls"] = []
# Phones in description
if result["description"]:
result["phones_found"] = extract_phones(result["description"])
except Exception:
pass
return result
def scrape(url: str) -> Dict[str, Any]:
"""
Entry point imported by main.py.
If url doesn't start with http, treat it as raw pasted text:
return it directly as description with all other fields null.
Returns dict: url, title, description, price_usd, image_urls, phones_found
"""
if not url.startswith("http"):
result = _empty(url)
result["description"] = url
result["phones_found"] = extract_phones(url)
return result
return asyncio.run(fetch_listing(url))
if __name__ == "__main__":
url = sys.stdin.readline().strip()
if url.startswith("http"):
data = asyncio.run(fetch_listing(url))
else:
data = scrape(url)
print(json.dumps(data, indent=2))