🕷️ The BEST Web Scraping framework that handles everything from a single request to a full-scale crawl! Don't be shy, join here: https://discord.gg/EMgGbDceNQ and follow here for daily tips and tricks: https://x.com/Scrapling_dev
Python aiai-scrapingautomationcrawlercrawlingcrawling-pythondatadata-extractionmcpmcp-serverplaywrightpythonscrapingselectorsstealthweb-scraperweb-scrapingweb-scraping-pythonwebscrapingxpath
fix(parser): match multi-class elements in find/find_all class_ filter
yetval69a2985ada9c
diff --git a/scrapling/parser.py b/scrapling/parser.pyindex 4638619..9366e74 100644--- a/scrapling/parser.py+++ b/scrapling/parser.py@@ -762,7 +762,11 @@ class Selector(SelectorsGeneration):for key, value in attributes.items():value = value.replace('"', r"\"") # Escape double quotes in user input# Not escaping anything with the key so the user can pass patterns like {'href*': '/p/'} or get errors :)- selector += '[{}="{}"]'.format(key, value)+ if key == "class":+ # `class` is a space-separated list, so exact-match [class="x"] misses class="x y"; match each name with ~=+ selector += "".join('[class~="{}"]'.format(t) for t in value.split())+ else:+ selector += '[{}="{}"]'.format(key, value)if selector != "*":selectors.append(selector)diff --git a/tests/parser/test_general.py b/tests/parser/test_general.pyindex 293e93e..7311ba2 100644--- a/tests/parser/test_general.py+++ b/tests/parser/test_general.py@@ -162,6 +162,20 @@ class TestSimilarElements:assert len(similar_high_rated_reviews) == 1+class TestFindByClass:+ def test_find_all_matches_multi_class_element(self, page):+ """A single class token should match elements that carry other classes too"""+ assert len(page.find_all("div", class_="stock")) == 3+ assert len(page.find_all("div", class_="hidden")) == 3+ assert page.find("div", class_="stock") is not None++ def test_find_all_matches_all_of_multiple_tokens(self, page):+ """Multiple class tokens all have to be present, regardless of order"""+ assert len(page.find_all("div", class_="hidden stock")) == 3+ assert len(page.find_all("div", class_="stock hidden")) == 3+ assert page.find_all("div", class_="hidden nonexistent") == []++# Error Handling Testsclass TestErrorHandling:def test_invalid_selector_initialization(self):