Skip to content

Commit a2061e8

Browse files
authored
Merge pull request #219 from scrapy-plugins/custom-attrs-docs
Add docs for CustomAttributes.
2 parents 47d3e1f + 06fc6c6 commit a2061e8

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/usage/scrapy-poet.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,46 @@ resulting page object:
131131
return Product(is_valid=False)
132132
return None
133133
134+
.. _custom-attrs:
135+
136+
Custom attribute extraction
137+
---------------------------
138+
139+
You can request custom attribute extraction by using either a
140+
:class:`scrapy_zyte_api.CustomAttributes` dependency (if you need both the
141+
attribute values and the attribute extraction metadata) or a
142+
:class:`scrapy_zyte_api.CustomAttributesValues` dependency (if you only need
143+
the values). You need to annotate it with input data as a dictionary and, if
144+
needed, a dictionary with extraction options. You should use the
145+
:func:`scrapy_zyte_api.custom_attrs` function to create the annotation:
146+
147+
.. code-block:: python
148+
149+
from typing import Annotated
150+
151+
from scrapy_zyte_api import CustomAttributes, custom_attrs
152+
153+
154+
@attrs.define
155+
class MyPageObject(BasePage):
156+
product: Product
157+
custom_attributes: Annotated[
158+
CustomAttributes,
159+
custom_attrs(
160+
{"name": {"type": "string", "description": "name of the product"}},
161+
{"method": "generate"},
162+
),
163+
]
164+
165+
You can then access the results as the dependency value:
166+
167+
.. code-block:: python
168+
169+
def parse_page(self, response: DummyResponse, page: MyPageObject):
170+
...
171+
for k, v in page.custom_attributes.items():
172+
...
173+
134174
135175
Custom parameters
136176
=================

0 commit comments

Comments
 (0)