From 036483ef548a7278b0f072815a9cc918e669b1b5 Mon Sep 17 00:00:00 2001 From: Mohammad Hijjawi Date: Sun, 15 Nov 2026 20:45:42 +0000 Subject: [PATCH] fix(nodes): match language indicators as path segments in SearchLinkNode The default language filter matched "/fr", "/de", "/it", ... as plain substrings of the path, so SearchLinkGraph dropped same-site links such as /details/42, /items/7, /free-trial or /developers. The "lang=" indicator was looked up among parse_qs keys, which never contain "=", so ?lang=es links were never filtered. Match "/xx" indicators only as whole path segments (optionally with a region suffix such as /pt-br/), and "name=" indicators against query parameter names. --- scrapegraphai/nodes/search_link_node.py | 24 +++++++++---- tests/nodes/search_link_node_filter_test.py | 37 +++++++++++++++++++++ 2 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 tests/nodes/search_link_node_filter_test.py diff --git a/scrapegraphai/nodes/search_link_node.py b/scrapegraphai/nodes/search_link_node.py index 4b1c02db4..95aff753f 100644 --- a/scrapegraphai/nodes/search_link_node.py +++ b/scrapegraphai/nodes/search_link_node.py @@ -76,12 +76,24 @@ def _is_language_url(self, url): lang_indicators = self.filter_config.get("lang_indicators", []) parsed_url = urlparse(url) - query_params = parse_qs(parsed_url.query) - - return any( - indicator in parsed_url.path.lower() or indicator in query_params - for indicator in lang_indicators - ) + query_params = {key.lower() for key in parse_qs(parsed_url.query)} + path = parsed_url.path.lower() + + for indicator in lang_indicators: + indicator = indicator.lower() + if indicator.endswith("="): + # "lang=" matches a "lang" query parameter + if indicator[:-1] in query_params: + return True + elif indicator.startswith("/"): + # "/fr" matches whole path segments such as /fr/ or /fr-ca/, + # not paths that merely start with it, like /free or /frameworks + code = re.escape(indicator.strip("/")) + if re.search(rf"/{code}(?:[-_][a-z]{{2}})?(?:/|$)", path): + return True + elif indicator in path or indicator in query_params: + return True + return False def _is_potentially_irrelevant(self, url): if not self.filter_links: diff --git a/tests/nodes/search_link_node_filter_test.py b/tests/nodes/search_link_node_filter_test.py new file mode 100644 index 000000000..262d625ab --- /dev/null +++ b/tests/nodes/search_link_node_filter_test.py @@ -0,0 +1,37 @@ +from langchain_core.documents import Document + +from scrapegraphai.nodes import SearchLinkNode + + +def _filtered_links(links, url="https://example.com"): + node = SearchLinkNode( + input="doc", + output=["parsed_doc"], + node_config={"filter_links": True}, + ) + state = {"doc": [Document(page_content=" ".join(links))], "url": url} + return node.execute(state)["parsed_doc"] + + +def test_language_filter_keeps_paths_that_only_start_with_a_language_code(): + links = [ + "https://example.com/details/42", + "https://example.com/items/7", + "https://example.com/free-trial", + "https://example.com/developers", + "https://example.com/esports", + ] + + assert sorted(_filtered_links(links)) == sorted(links) + + +def test_language_filter_drops_language_paths_and_lang_query(): + links = [ + "https://example.com/fr", + "https://example.com/fr/produits", + "https://example.com/pt-br/produtos", + "https://example.com/de_de/produkte", + "https://example.com/products?lang=es", + ] + + assert _filtered_links(links) == []