File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff 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=================
You can’t perform that action at this time.
0 commit comments