Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions scrapegraphai/nodes/search_link_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
37 changes: 37 additions & 0 deletions tests/nodes/search_link_node_filter_test.py
Original file line number Diff line number Diff line change
@@ -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) == []
Loading