2020-06-17 23:37:01 +02:00
|
|
|
LET baseURL = 'https://www.amazon.com/'
|
|
|
|
LET amazon = DOCUMENT(baseURL, { driver: "cdp" })
|
2018-10-09 02:20:40 +02:00
|
|
|
|
|
|
|
INPUT(amazon, '#twotabsearchtextbox', @criteria)
|
|
|
|
CLICK(amazon, '.nav-search-submit input[type="submit"]')
|
|
|
|
WAIT_NAVIGATION(amazon)
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
LET resultListSelector = 'div.s-result-list'
|
2020-06-17 23:37:01 +02:00
|
|
|
LET resultItemSelector = '[data-component-type="s-search-result"]'
|
2019-06-19 23:58:56 +02:00
|
|
|
LET nextBtnSelector = 'ul.a-pagination .a-last a'
|
2020-06-17 23:37:01 +02:00
|
|
|
LET priceWholeSelector = '.a-price-whole'
|
|
|
|
LET priceFracSelector = '.a-price-fraction'
|
2019-06-19 23:58:56 +02:00
|
|
|
LET pagers = ELEMENTS(amazon, 'ul.a-pagination li.a-disabled')
|
|
|
|
LET pages = LENGTH(pagers) > 0 ? TO_INT(INNER_TEXT(LAST(pagers))) : 0
|
2019-01-12 04:25:58 +02:00
|
|
|
|
2018-10-09 02:20:40 +02:00
|
|
|
LET result = (
|
2019-01-12 04:08:59 +02:00
|
|
|
FOR pageNum IN 1..pages
|
2020-06-17 23:37:01 +02:00
|
|
|
LIMIT @pages
|
|
|
|
|
2018-10-09 02:20:40 +02:00
|
|
|
LET clicked = pageNum == 1 ? false : CLICK(amazon, nextBtnSelector)
|
2020-06-17 23:37:01 +02:00
|
|
|
LET wait = clicked ? WAIT_NAVIGATION(amazon, 10000) : false
|
2018-10-09 02:20:40 +02:00
|
|
|
LET waitSelector = wait ? WAIT_ELEMENT(amazon, resultListSelector) : false
|
|
|
|
|
2019-06-19 23:58:56 +02:00
|
|
|
PRINT("page:", pageNum, "clicked", clicked)
|
|
|
|
|
2018-10-09 02:20:40 +02:00
|
|
|
LET items = (
|
|
|
|
FOR el IN ELEMENTS(amazon, resultItemSelector)
|
2020-06-17 23:37:01 +02:00
|
|
|
LET hasPrice = ELEMENT_EXISTS(el, priceWholeSelector)
|
|
|
|
LET priceWholeTxt = hasPrice ? FIRST(REGEX_MATCH(INNER_TEXT(el, priceWholeSelector), "[0-9]+")) : "0"
|
|
|
|
LET priceFracTxt = hasPrice ? FIRST(REGEX_MATCH(INNER_TEXT(el, priceFracSelector), "[0-9]+")) : "00"
|
|
|
|
LET price = TO_FLOAT(priceWholeTxt + "." + priceFracTxt)
|
|
|
|
LET anchor = ELEMENT(el, "a")
|
2018-10-11 18:39:03 +02:00
|
|
|
|
|
|
|
RETURN {
|
2020-06-17 23:37:01 +02:00
|
|
|
url: baseURL + anchor.attributes.href,
|
2018-10-11 18:39:03 +02:00
|
|
|
title: INNER_TEXT(el, 'h2'),
|
2019-01-12 19:21:38 +02:00
|
|
|
price
|
2018-10-11 18:39:03 +02:00
|
|
|
}
|
2018-10-09 02:20:40 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
RETURN items
|
|
|
|
)
|
|
|
|
|
2019-01-12 04:08:59 +02:00
|
|
|
RETURN FLATTEN(result)
|