forked from zytedata/zyte-common-items
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessors.py
More file actions
416 lines (324 loc) · 13.1 KB
/
processors.py
File metadata and controls
416 lines (324 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
from collections.abc import Iterable, Mapping
from functools import wraps
from numbers import Real
from typing import Any, Callable, List, Optional, Union
from clear_html import clean_node, cleaned_node_to_html, cleaned_node_to_text
from lxml.html import HtmlElement
from parsel import Selector, SelectorList
from price_parser import Price
from web_poet.mixins import ResponseShortcutsMixin
from zyte_parsers import Breadcrumb as zp_Breadcrumb
from zyte_parsers import Gtin as zp_Gtin
from zyte_parsers import (
extract_brand_name,
extract_breadcrumbs,
extract_gtin,
extract_price,
extract_rating,
extract_review_count,
)
from .components import (
AggregateRating,
BaseMetadata,
Brand,
Breadcrumb,
Gtin,
Image,
ProbabilityRequest,
Request,
)
def _get_base_url(page: Any) -> Optional[str]:
if isinstance(page, ResponseShortcutsMixin):
return page.base_url
return getattr(page, "url", None)
def _handle_selectorlist(value: Any) -> Any:
if not isinstance(value, SelectorList):
return value
if len(value) == 0:
return None
return value[0]
def _format_price(price: Price) -> Optional[str]:
"""Return the price amount as a string, with a minimum of 2 decimal
places."""
if price.amount is None:
return None
*_, exponent = price.amount.as_tuple()
if not isinstance(exponent, int):
return None # NaN, Infinity, etc.
if exponent <= -2:
return str(price.amount)
return f"{price.amount:.2f}"
def only_handle_nodes(
f: Callable[[Union[Selector, HtmlElement], Any], Any]
) -> Callable[[Any, Any], Any]:
"""Decorator for processors that only runs a decorated processor if the
input is of type :class:`Selector` or :class:`HtmlElement`."""
@wraps(f)
def wrapper(value: Any, page: Any) -> Any:
value = _handle_selectorlist(value)
if not isinstance(value, (Selector, HtmlElement)):
return value
result = f(value, page)
return result
return wrapper
def breadcrumbs_processor(value: Any, page: Any) -> Any:
"""Convert the data into a list of :class:`~zyte_common_items.Breadcrumb` objects if possible.
Supported inputs are :class:`~parsel.selector.Selector`,
:class:`~parsel.selector.SelectorList`, :class:`~lxml.html.HtmlElement` and
an iterable of :class:`zyte_parsers.Breadcrumb` objects. Other inputs are
returned as is.
"""
def _from_zp_breadcrumb(value: zp_Breadcrumb) -> Breadcrumb:
return Breadcrumb(name=value.name, url=value.url)
value = _handle_selectorlist(value)
if isinstance(value, (Selector, HtmlElement)):
zp_breadcrumbs = extract_breadcrumbs(value, base_url=_get_base_url(page))
return (
[_from_zp_breadcrumb(b) for b in zp_breadcrumbs] if zp_breadcrumbs else None
)
if not isinstance(value, Iterable) or isinstance(value, str):
return value
results: List[Any] = []
for item in value:
if isinstance(item, zp_Breadcrumb):
results.append(_from_zp_breadcrumb(item))
else:
results.append(item)
return results
def brand_processor(value: Any, page: Any) -> Any:
"""Convert the data into a brand name if possible.
If inputs are either :class:`~parsel.selector.Selector`,
:class:`~parsel.selector.SelectorList` or :class:`~lxml.html.HtmlElement`, attempts
to extract brand data from it.
If value is a string, uses it to create a :class:`~zyte_common_items.Brand` instance.
Other inputs are returned unchanged.
"""
value = _handle_selectorlist(value)
if isinstance(value, str):
return Brand(name=value) if value else None
if isinstance(value, (Selector, SelectorList, HtmlElement)):
if brand_name := extract_brand_name(value, search_depth=2):
return Brand(name=brand_name)
else:
return None
return value
def price_processor(value: Any, page: Any) -> Any:
"""Convert the data into a price string if possible.
Uses the price-parser_ library.
Supported inputs are :class:`~parsel.selector.Selector`,
:class:`~parsel.selector.SelectorList`, :class:`~lxml.html.HtmlElement` and numeric values.
Other inputs are returned as is.
Puts the parsed Price object into ``page._parsed_price``.
.. _price-parser: https://github.com/scrapinghub/price-parser
"""
value = _handle_selectorlist(value)
if isinstance(value, Real):
return f"{value:.2f}"
elif isinstance(value, (Selector, HtmlElement)):
price = extract_price(value)
page._parsed_price = price
return _format_price(price)
else:
return value
def simple_price_processor(value: Any, page: Any) -> Any:
"""Convert the data into a price string if possible.
Uses the price-parser_ library.
Supported inputs are :class:`~parsel.selector.Selector`,
:class:`~parsel.selector.SelectorList`, :class:`~lxml.html.HtmlElement` and numeric values.
Other inputs are returned as is.
.. _price-parser: https://github.com/scrapinghub/price-parser
"""
value = _handle_selectorlist(value)
if isinstance(value, Real):
return f"{value:.2f}"
elif isinstance(value, (Selector, HtmlElement)):
price = extract_price(value)
return _format_price(price)
else:
return value
@only_handle_nodes
def description_html_processor(value: Union[Selector, HtmlElement], page: Any) -> Any:
"""Convert the data into a cleaned up HTML if possible.
Uses the clear-html_ library.
Supported inputs are :class:`~parsel.selector.Selector`,
:class:`~parsel.selector.SelectorList` and :class:`~lxml.html.HtmlElement`.
Other inputs are returned as is.
Puts the cleaned HtmlElement object into ``page._descriptionHtml_node``.
.. _clear-html: https://github.com/zytedata/clear-html
"""
if isinstance(value, Selector):
value = value.root
if value is None:
return None
if not isinstance(value, HtmlElement):
raise ValueError(
f"description_html_processor expects an HtmlElement node, got "
f"{value.__class__}"
)
cleaned_node = clean_node(value, _get_base_url(page))
page._descriptionHtml_node = cleaned_node
return cleaned_node_to_html(cleaned_node)
def description_processor(value: Any, page: Any) -> Any:
"""Convert the data into a cleaned up text if possible.
Uses the clear-html_ library.
Supported inputs are :class:`~parsel.selector.Selector`,
:class:`~parsel.selector.SelectorList` and :class:`~lxml.html.HtmlElement`.
Other inputs are returned as is.
Puts the cleaned HtmlElement object into ``page._description_node`` and the
cleaned text into ``page._description_str``.
.. _clear-html: https://github.com/zytedata/clear-html
"""
value = _handle_selectorlist(value)
if isinstance(value, str):
page._description_str = value
return value
if isinstance(value, Selector):
value = value.root
if value is None:
return None
if not isinstance(value, HtmlElement):
raise ValueError(
f"description_processor expects an HtmlElement node, got "
f"{value.__class__}"
)
cleaned_node = clean_node(value, _get_base_url(page))
cleaned_text = cleaned_node_to_text(cleaned_node)
page._description_node = cleaned_node
page._description_str = cleaned_text
return cleaned_text
def gtin_processor(
value: Union[SelectorList, Selector, HtmlElement, str], page: Any
) -> Any:
"""Convert the data into a list of :class:`~zyte_common_items.Gtin` objects if possible.
Supported inputs are :class:`str`, :class:`~parsel.selector.Selector`,
:class:`~parsel.selector.SelectorList`, :class:`~lxml.html.HtmlElement`, an
iterable of :class:`str` and an iterable of :class:`zyte_parsers.Gtin`
objects.
Other inputs are returned as is.
"""
def _from_zp_gtin(zp_value: zp_Gtin) -> Gtin:
return Gtin(type=zp_value.type, value=zp_value.value)
results = []
if isinstance(value, SelectorList):
for sel in value:
if result := extract_gtin(sel):
results.append(_from_zp_gtin(result))
elif isinstance(value, (Selector, HtmlElement, str)):
if result := extract_gtin(value):
results.append(_from_zp_gtin(result))
elif isinstance(value, Iterable):
for item in value:
if isinstance(item, zp_Gtin):
results.append(_from_zp_gtin(item))
elif isinstance(item, str):
results.append(_from_zp_gtin(extract_gtin(item)))
else:
results.append(item)
else:
return value
return results or None
def rating_processor(value: Any, page: Any) -> Any:
"""Convert the data into an :class:`~zyte_common_items.AggregateRating`
object if possible.
Supported inputs are selector-like objects
(:class:`~parsel.selector.Selector`,
:class:`~parsel.selector.SelectorList`, or
:class:`~lxml.html.HtmlElement`).
The input can also be a dictionary with one or more of the
:class:`~zyte_common_items.AggregateRating` fields as keys. The values for
those keys can be either final values, to be assigned to the corresponding
fields, or selector-like objects.
If a returning dictionary is missing the ``bestRating`` field and
``ratingValue`` is a selector-like object, ``bestRating`` may be extracted.
For example, for the following input HTML:
.. code-block:: html
<span class="rating">3.8 out of 5 stars</span>
<a class="reviews">See all 7 reviews</a>
You can use:
.. code-block:: python
@field
def aggregateRating(self):
return {
"ratingValue": self.css(".rating"),
"reviewCount": self.css(".reviews"),
}
To get:
.. code-block:: python
AggregateRating(
bestRating=5.0,
ratingValue=3.8,
reviewCount=7,
)
"""
value = _handle_selectorlist(value)
if isinstance(value, (Selector, HtmlElement)):
zp_rating = extract_rating(value)
result = AggregateRating(
reviewCount=extract_review_count(value),
bestRating=zp_rating.bestRating,
ratingValue=zp_rating.ratingValue,
)
if result.reviewCount or result.bestRating or result.ratingValue:
return result
return None
elif isinstance(value, dict):
result = AggregateRating()
review_count = _handle_selectorlist(value.get("reviewCount"))
if isinstance(review_count, (Selector, HtmlElement)):
result.reviewCount = extract_review_count(review_count)
elif review_count is not None:
result.reviewCount = int(review_count)
rating_value = _handle_selectorlist(value.get("ratingValue"))
if isinstance(rating_value, (Selector, HtmlElement)):
zp_rating = extract_rating(rating_value)
result.ratingValue = zp_rating.ratingValue
result.bestRating = zp_rating.bestRating
elif rating_value is not None:
result.ratingValue = float(rating_value)
if (best_rating := value.get("bestRating")) is not None:
result.bestRating = float(best_rating)
if result.reviewCount or result.bestRating or result.ratingValue:
return result
return None
return value
def images_processor(value: Any, page: Any) -> Any:
"""Convert the data into a list of :class:`~zyte_common_items.Image`
objects if possible.
If the input is a string, it's used as a url for returning image object.
If input is either an iterable of strings or mappings with "url" key, they are
used to populate image objects.
Other inputs are returned unchanged.
"""
# TODO: add generic-purpose extract_images utility to zyte-parsers
#
# value = _handle_selectorlist(value)
# if isinstance(value, (Selector, HtmlElement)):
# images = extract_images(value)
# return [Image(url=url) for url in images]
if isinstance(value, str):
return [Image(url=value)]
if isinstance(value, Iterable):
results: List[Any] = []
for item in value:
if isinstance(item, Image):
results.append(item)
elif isinstance(item, Mapping):
if url := item.get("url"):
results.append(Image(url=url))
elif isinstance(item, str):
results.append(Image(url=item))
return results
return value
def probability_request_list_processor(
request_list: List[Request],
) -> List[ProbabilityRequest]:
"""Convert all objects in *request_list*, which are instances of
:class:`Request` or a subclass, into instances of
:class:`ProbabilityRequest`."""
return [request.cast(ProbabilityRequest) for request in request_list]
def metadata_processor(metadata: BaseMetadata, page):
"""Processor for a metadata field that ensures that the output metadata
object uses the metadata class declared by *page*."""
if page.metadata_cls is None:
return None
return metadata.cast(page.metadata_cls)