-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from Gallaecio/product-list-product-extractor
Provide extractors
- Loading branch information
Showing
8 changed files
with
222 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.. _extractor-api: | ||
|
||
============= | ||
Extractor API | ||
============= | ||
|
||
API reference of provided :ref:`extractors <extractors>`. | ||
|
||
Product from list | ||
================= | ||
|
||
.. autoclass:: zyte_common_items.ProductFromListExtractor | ||
|
||
.. autoclass:: zyte_common_items.ProductFromListSelectorExtractor | ||
|
||
|
||
Product variant | ||
=============== | ||
|
||
.. autoclass:: zyte_common_items.ProductVariantExtractor | ||
|
||
.. autoclass:: zyte_common_items.ProductVariantSelectorExtractor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ Reference | |
|
||
items | ||
pages | ||
extractors | ||
components | ||
adapter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import attrs | ||
import pytest | ||
from parsel import Selector | ||
from web_poet import field | ||
|
||
from zyte_common_items import ( | ||
ProductFromList, | ||
ProductFromListExtractor, | ||
ProductFromListSelectorExtractor, | ||
ProductVariant, | ||
ProductVariantExtractor, | ||
ProductVariantSelectorExtractor, | ||
) | ||
|
||
from .test_processors import gtin_expected, gtin_str | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_product_from_list_extractor(): | ||
@attrs.define | ||
class MyProductFromListExtractor(ProductFromListExtractor): | ||
selector: Selector | ||
|
||
@field | ||
def price(self): | ||
return self.selector.css("price") | ||
|
||
@field | ||
def regularPrice(self): | ||
return self.selector.css("oldPrice") | ||
|
||
selector = Selector("<data><price>10€</price><oldPrice>20€</oldPrice></data>") | ||
extracted = await MyProductFromListExtractor(selector).to_item() | ||
|
||
assert isinstance(extracted, ProductFromList) | ||
assert extracted.price == "10.00" | ||
assert extracted.regularPrice == "20.00" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_product_from_list_selector_extractor(): | ||
class MyProductFromListSelectorExtractor(ProductFromListSelectorExtractor): | ||
@field | ||
def price(self): | ||
return self.css("price") | ||
|
||
@field | ||
def regularPrice(self): | ||
return self.css("oldPrice") | ||
|
||
selector = Selector("<data><price>10€</price><oldPrice>20€</oldPrice></data>") | ||
extracted = await MyProductFromListSelectorExtractor(selector).to_item() | ||
|
||
assert isinstance(extracted, ProductFromList) | ||
assert extracted.price == "10.00" | ||
assert extracted.regularPrice == "20.00" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_product_variant_extractor(): | ||
@attrs.define | ||
class MyProductVariantExtractor(ProductVariantExtractor): | ||
selector: Selector | ||
|
||
@field | ||
def gtin(self): | ||
return self.selector.css("gtin") | ||
|
||
@field | ||
def price(self): | ||
return self.selector.css("price") | ||
|
||
@field | ||
def regularPrice(self): | ||
return self.selector.css("oldPrice") | ||
|
||
selector = Selector( | ||
f"<data>" | ||
f"<price>10€</price>" | ||
f"<oldPrice>20€</oldPrice>" | ||
f"<unusedField>foo</unusedField>" | ||
f"<gtin>{gtin_str}</gtin>" | ||
f"</data>" | ||
) | ||
extracted = await MyProductVariantExtractor(selector).to_item() | ||
|
||
assert isinstance(extracted, ProductVariant) | ||
assert extracted.gtin == gtin_expected | ||
assert extracted.price == "10.00" | ||
assert extracted.regularPrice == "20.00" | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_product_variant_selector_extractor(): | ||
class MyProductVariantSelectorExtractor(ProductVariantSelectorExtractor): | ||
@field | ||
def gtin(self): | ||
return self.css("gtin") | ||
|
||
@field | ||
def price(self): | ||
return self.css("price") | ||
|
||
@field | ||
def regularPrice(self): | ||
return self.css("oldPrice") | ||
|
||
selector = Selector( | ||
f"<data>" | ||
f"<price>10€</price>" | ||
f"<oldPrice>20€</oldPrice>" | ||
f"<unusedField>foo</unusedField>" | ||
f"<gtin>{gtin_str}</gtin>" | ||
f"</data>" | ||
) | ||
extracted = await MyProductVariantSelectorExtractor(selector).to_item() | ||
|
||
assert isinstance(extracted, ProductVariant) | ||
assert extracted.gtin == gtin_expected | ||
assert extracted.price == "10.00" | ||
assert extracted.regularPrice == "20.00" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from web_poet import Extractor, SelectorExtractor | ||
|
||
from .items import ProductFromList, ProductVariant | ||
from .processors import gtin_processor, price_processor, simple_price_processor | ||
|
||
|
||
class _ProductProcessors: | ||
price = [price_processor] | ||
regularPrice = [simple_price_processor] | ||
|
||
|
||
class ProductFromListExtractor(Extractor[ProductFromList]): | ||
""":class:`~web_poet.pages.Extractor` for :class:`ProductFromList`.""" | ||
|
||
class Processors(_ProductProcessors): | ||
pass | ||
|
||
|
||
class ProductFromListSelectorExtractor(SelectorExtractor[ProductFromList]): | ||
""":class:`~web_poet.pages.SelectorExtractor` for | ||
:class:`ProductFromList`.""" | ||
|
||
class Processors(_ProductProcessors): | ||
pass | ||
|
||
|
||
class _ProductVariantProcessors(_ProductProcessors): | ||
gtin = [gtin_processor] | ||
|
||
|
||
class ProductVariantExtractor(Extractor[ProductVariant]): | ||
""":class:`~web_poet.pages.Extractor` for :class:`ProductVariant`.""" | ||
|
||
class Processors(_ProductVariantProcessors): | ||
pass | ||
|
||
|
||
class ProductVariantSelectorExtractor(SelectorExtractor[ProductVariant]): | ||
""":class:`~web_poet.pages.SelectorExtractor` for | ||
:class:`ProductVariant`.""" | ||
|
||
class Processors(_ProductVariantProcessors): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters