diff --git a/engine.py b/engine.py index 491ad39..2876168 100644 --- a/engine.py +++ b/engine.py @@ -37,13 +37,12 @@ _CHALLENGE_MARKS = [ def is_challenge_page(title, html): + """判断是否仍在反爬验证页 (仅关键词启发, 避免误伤正常小页面)""" low = html.lower() t = (title or "").lower() for mark in _CHALLENGE_MARKS: if mark in t or mark in low: return True - if len(html) < 5000 and (" 1 and path.endswith("/"): + path = path.rstrip("/") + query = "" + if p.query: + kept = [kv for kv in p.query.split("&") + if kv.split("=", 1)[0].lower() not in _TRACKING_PARAMS] + if kept: + query = "?" + "&".join(kept) + return f"{p.scheme.lower()}://{host}{port}{path}{query}" + except Exception: + return str(url) + + def filter_links(hrefs, seed_url, include=None, exclude=None, same_domain=True, use_regex=False): """按规则过滤链接, 返回 (included, excluded); excluded 含排除原因""" @@ -551,18 +580,19 @@ class CrawlJob: p, browser, ctx, page, cookie_file = self._open_browser() queue = [(seed, 0, "")] # (url, depth, 来源链接) - visited = set() - queued = set([seed]) + visited = set() # 规范化 URL 去重 + queued = set([normalize_url(seed)]) idx = 0 try: while queue and not self._stop.is_set(): self._wait_if_paused() url, depth, src = queue.pop(0) - if url in visited: + key = normalize_url(url) + if key in visited: continue if len(visited) >= max_pages: break - visited.add(url) + visited.add(key) idx += 1 run["progress"]["current_url"] = url run["progress"]["done"] = len(visited) @@ -574,8 +604,9 @@ class CrawlJob: self._persist() if entry["status"] == "OK" and depth < max_depth: for link in self._discover_links(page): - if link not in visited and link not in queued: - queued.add(link) + lk = normalize_url(link) + if lk not in visited and lk not in queued: + queued.add(lk) queue.append((link, depth + 1, url)) if entry["status"] == "OK": self._delay()