ebay.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Ebay (Videos, Music, Files)
  4. """
  5. from urllib.parse import quote
  6. from lxml import html
  7. from searx.engines.xpath import extract_text
  8. # about
  9. about = {
  10. "website": 'https://www.ebay.com',
  11. "wikidata_id": 'Q58024',
  12. "official_api_documentation": 'https://developer.ebay.com/',
  13. "use_official_api": False,
  14. "require_api_key": False,
  15. "results": 'HTML',
  16. }
  17. categories = ['shopping']
  18. paging = True
  19. # Set base_url in settings.yml in order to
  20. # have the desired local TLD.
  21. base_url = None
  22. search_url = '/sch/i.html?_nkw={query}&_sacat={pageno}'
  23. results_xpath = '//li[contains(@class, "s-item")]'
  24. url_xpath = './/a[@class="s-item__link"]/@href'
  25. title_xpath = './/h3[@class="s-item__title"]'
  26. content_xpath = './/div[@span="SECONDARY_INFO"]'
  27. price_xpath = './/div[contains(@class, "s-item__detail")]/span[@class="s-item__price"][1]/text()'
  28. shipping_xpath = './/span[contains(@class, "s-item__shipping")]/text()'
  29. source_country_xpath = './/span[contains(@class, "s-item__location")]/text()'
  30. thumbnail_xpath = './/img[@class="s-item__image-img"]/@src'
  31. def request(query, params):
  32. params['url'] = f'{base_url}' + search_url.format(query=quote(query), pageno=params['pageno'])
  33. return params
  34. def response(resp):
  35. results = []
  36. dom = html.fromstring(resp.text)
  37. results_dom = dom.xpath(results_xpath)
  38. if not results_dom:
  39. return []
  40. for result_dom in results_dom:
  41. url = extract_text(result_dom.xpath(url_xpath))
  42. title = extract_text(result_dom.xpath(title_xpath))
  43. content = extract_text(result_dom.xpath(content_xpath))
  44. price = extract_text(result_dom.xpath(price_xpath))
  45. shipping = extract_text(result_dom.xpath(shipping_xpath))
  46. source_country = extract_text(result_dom.xpath(source_country_xpath))
  47. thumbnail = extract_text(result_dom.xpath(thumbnail_xpath))
  48. if title == "":
  49. continue
  50. results.append(
  51. {
  52. 'url': url,
  53. 'title': title,
  54. 'content': content,
  55. 'price': price,
  56. 'shipping': shipping,
  57. 'source_country': source_country,
  58. 'thumbnail': thumbnail,
  59. 'template': 'products.html',
  60. }
  61. )
  62. return results