-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkfilter.py
More file actions
220 lines (187 loc) · 8.29 KB
/
Copy pathlinkfilter.py
File metadata and controls
220 lines (187 loc) · 8.29 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
# DOESN'T NEED TO BE TOUCHED KEEP EXACTLY AS IS
# ---- DO NOT TOUCH ----
import json
import re
import time
import requests
from urllib.parse import urlparse, urljoin
from bs4 import BeautifulSoup
import tldextract
TIMEOUT = 6
HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/117.0.0.0 Safari/537.36"}
# Weights for scoring — tuneable
WEIGHTS = {
"has_phone": 45,
"has_email": 18,
"has_contact_link": 20,
"schema_localbusiness": 55,
"biz_kw_in_title": 22,
"biz_kw_in_meta": 12,
"domain_keyword": 18,
"short_brand_domain": 10,
"junk_kw_in_title": -36,
"long_title_penalty": -10,
"path_is_article": -18,
"publisher_domain_penalty": -999 # immediate reject for known publishers
}
# Blacklists
PUBLISHER_DOMAINS = {
"yelp.com","tripadvisor.com","timeout.com","eater.com","latimes.com",
"sfchronicle.com","nytimes.com","forbes.com","wikipedia.org","angieslist.com",
"thumbtack.com","expertise.com","homeadvisor.com","broadly.com"
}
JUNK_PATH_KEYWORDS = {"blog","article","news","review","story","guide","list","best","top","ranking","magazine","press","insight"}
JUNK_TITLE_KEYWORDS = {"top","best","guide","review","list","rank","ranking","how to","how-to","things to do","things to see"}
BUSINESS_KEYWORDS = {"inc","llc","company","co.","roof","roofing","plumb","plumber","plumbing","dentist","dental","restaurant","cafe","bar","salon","spa","contractor","contracting","services","service","shop","store","clinic","bakery","hotel"}
GOOD_TLDS = {".com", ".net", ".org", ".biz", ".co", ".restaurant"}
EMAIL_RE = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}", re.I)
PHONE_RE = re.compile(r"(\+?\d[\d\-\.\s\(\)]{6,}\d)")
def get_root_domain(url):
parsed = urlparse(url)
ext = tldextract.extract(parsed.netloc)
if ext.domain and ext.suffix:
return f"{ext.domain}.{ext.suffix}"
return parsed.netloc.lower()
def fetch_page(url):
"""Fetch page text (with a short timeout). returns (text, final_url) or (None, None)"""
try:
r = requests.get(url, timeout=TIMEOUT, headers=HEADERS)
if r.status_code == 200 and 'text/html' in r.headers.get('Content-Type',''):
return r.text, r.url
except Exception:
return None, None
return None, None
def extract_title_meta(html):
soup = BeautifulSoup(html, "html.parser")
title = (soup.title.string or "").strip() if soup.title else ""
desc_tag = soup.find("meta", attrs={"name": "description"}) or soup.find("meta", attrs={"property": "og:description"})
meta = desc_tag["content"].strip() if desc_tag and desc_tag.has_attr("content") else ""
return title, meta
def find_contact_links(html, base_url):
soup = BeautifulSoup(html, "html.parser")
links = []
for a in soup.select("a[href]"):
href = a["href"].lower()
text = (a.get_text(" ", strip=True) or "").lower()
if "contact" in href or "contact" in text or "book" in href or "appointment" in text or "reserve" in href:
links.append(urljoin(base_url, a["href"]))
return links
def has_schema_localbusiness(html):
soup = BeautifulSoup(html, "html.parser")
for script in soup.find_all("script", type="application/ld+json"):
try:
txt = script.string or ""
if "LocalBusiness" in txt or "Organization" in txt:
return True
except Exception:
continue
return False
def count_phone_email(html):
emails = set(EMAIL_RE.findall(html or ""))
phones = set(PHONE_RE.findall(html or ""))
# lightweight cleanup
phones = {p for p in phones if len(re.sub(r'\D','',p)) >= 7}
return len(phones), len(emails)
def score_domain(domain, sample_url=None):
if any(pub in domain for pub in PUBLISHER_DOMAINS):
return {"domain": domain, "score": WEIGHTS["publisher_domain_penalty"], "reason": "publisher-domain", "kept_url": None}
homepage = f"https://{domain}"
pages_to_try = [homepage]
if sample_url:
pages_to_try.insert(0, sample_url)
best_score = -9999
best_features = None
kept_url = None
for url in pages_to_try:
html, final_url = fetch_page(url)
if html is None:
continue
title, meta = extract_title_meta(html)
title_l = title.lower()
meta_l = meta.lower()
combined = f"{title_l} {meta_l}"
# base score
score = 0
features = {}
if has_schema_localbusiness(html):
score += WEIGHTS["schema_localbusiness"]
features["schema"] = True
contact_links = find_contact_links(html, final_url)
if contact_links:
score += WEIGHTS["has_contact_link"]
features["contact_links"] = contact_links[:3]
phone_count, email_count = count_phone_email(html)
if phone_count:
score += WEIGHTS["has_phone"]
features["phone_count"] = phone_count
if email_count:
score += WEIGHTS["has_email"]
features["email_count"] = email_count
if any(kw in combined for kw in BUSINESS_KEYWORDS):
score += WEIGHTS["biz_kw_in_title"]
features["biz_kw"] = True
if any(kw in meta_l for kw in BUSINESS_KEYWORDS):
score += WEIGHTS["biz_kw_in_meta"]
if any(kw in domain for kw in ["roof","plumb","plumbing","clinic","salon","dent","restaurant","cafe","hvac","electric"]):
score += WEIGHTS["domain_keyword"]
features["domain_kw"] = True
nd = domain.split(".")[0]
if len(nd) <= 20 and "." not in nd and len(domain) < 25:
score += WEIGHTS["short_brand_domain"]
features["brand_like"] = True
if any(jk in combined for jk in JUNK_TITLE_KEYWORDS):
score += WEIGHTS["junk_kw_in_title"]
features["junk_title"] = True
parsed = urlparse(final_url)
path = parsed.path.lower()
if any(p in path for p in JUNK_PATH_KEYWORDS) and not any(k in domain for k in ["roof","plumb","restaurant","cafe","clinic"]):
score += WEIGHTS["path_is_article"]
features["path_is_article"] = True
title_word_count = len(title.split())
if title_word_count > 12 and not any(k in domain for k in ["roof","plumb","clinic","restaurant"]):
score += WEIGHTS["long_title_penalty"]
features["long_title_words"] = title_word_count
if score > best_score:
best_score = score
best_features = {"title": title, "meta": meta, "url": final_url, **features}
kept_url = final_url
time.sleep(0.2)
if best_features is None:
return {"domain": domain, "score": -100, "reason": "fetch-failed", "kept_url": None}
return {"domain": domain, "score": best_score, "features": best_features, "kept_url": kept_url}
def select_business_sites(scraped_links, want=10, max_domains_to_check=60):
domain_to_sample = {}
for url in scraped_links:
d = get_root_domain(url)
if d not in domain_to_sample:
domain_to_sample[d] = url
domains = list(domain_to_sample.items())[:max_domains_to_check]
results = []
for domain, sample in domains:
res = score_domain(domain, sample_url=sample)
results.append(res)
results_sorted = sorted(results, key=lambda x: x.get("score", -9999), reverse=True)
selected = []
for r in results_sorted:
if len(selected) >= want:
break
if r.get("score", -9999) >= 15:
selected.append(r)
idx = 0
while len(selected) < want and idx < len(results_sorted):
candidate = results_sorted[idx]
if candidate not in selected:
selected.append(candidate)
idx += 1
return {"candidates": results_sorted, "selected": selected}
if __name__ == "__main__":
with open("scraped_links.json", "r", encoding="utf-8") as f:
scraped = json.load(f)
out = select_business_sites(scraped, want=10, max_domains_to_check=80)
with open("domain_scores_debug.json", "w", encoding="utf-8") as f:
json.dump(out, f, indent=2)
with open("selected_sites.json", "w", encoding="utf-8") as f:
json.dump([site["kept_url"] for site in out["selected"] if site.get("kept_url")], f, indent=2)
# ---- DO NOT TOUCH ----