xpath.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from lxml import html
  2. from urllib.parse import urlencode
  3. from searx.utils import extract_text, extract_url, eval_xpath
  4. search_url = None
  5. url_xpath = None
  6. content_xpath = None
  7. title_xpath = None
  8. thumbnail_xpath = False
  9. paging = False
  10. suggestion_xpath = ''
  11. results_xpath = ''
  12. cached_xpath = ''
  13. cached_url = ''
  14. # parameters for engines with paging support
  15. #
  16. # number of results on each page
  17. # (only needed if the site requires not a page number, but an offset)
  18. page_size = 1
  19. # number of the first page (usually 0 or 1)
  20. first_page_num = 1
  21. def request(query, params):
  22. query = urlencode({'q': query})[2:]
  23. fp = {'query': query}
  24. if paging and search_url.find('{pageno}') >= 0:
  25. fp['pageno'] = (params['pageno'] - 1) * page_size + first_page_num
  26. params['url'] = search_url.format(**fp)
  27. params['query'] = query
  28. return params
  29. def response(resp):
  30. results = []
  31. dom = html.fromstring(resp.text)
  32. is_onion = True if 'onions' in categories else False
  33. if results_xpath:
  34. for result in eval_xpath(dom, results_xpath):
  35. url = extract_url(eval_xpath(result, url_xpath), search_url)
  36. title = extract_text(eval_xpath(result, title_xpath))
  37. content = extract_text(eval_xpath(result, content_xpath))
  38. tmp_result = {'url': url, 'title': title, 'content': content}
  39. # add thumbnail if available
  40. if thumbnail_xpath:
  41. thumbnail_xpath_result = eval_xpath(result, thumbnail_xpath)
  42. if len(thumbnail_xpath_result) > 0:
  43. tmp_result['img_src'] = extract_url(thumbnail_xpath_result, search_url)
  44. # add alternative cached url if available
  45. if cached_xpath:
  46. tmp_result['cached_url'] = cached_url + extract_text(result.xpath(cached_xpath))
  47. if is_onion:
  48. tmp_result['is_onion'] = True
  49. results.append(tmp_result)
  50. else:
  51. if cached_xpath:
  52. for url, title, content, cached in zip(
  53. (extract_url(x, search_url) for
  54. x in dom.xpath(url_xpath)),
  55. map(extract_text, dom.xpath(title_xpath)),
  56. map(extract_text, dom.xpath(content_xpath)),
  57. map(extract_text, dom.xpath(cached_xpath))
  58. ):
  59. results.append({'url': url, 'title': title, 'content': content,
  60. 'cached_url': cached_url + cached, 'is_onion': is_onion})
  61. else:
  62. for url, title, content in zip(
  63. (extract_url(x, search_url) for
  64. x in dom.xpath(url_xpath)),
  65. map(extract_text, dom.xpath(title_xpath)),
  66. map(extract_text, dom.xpath(content_xpath))
  67. ):
  68. results.append({'url': url, 'title': title, 'content': content, 'is_onion': is_onion})
  69. if not suggestion_xpath:
  70. return results
  71. for suggestion in eval_xpath(dom, suggestion_xpath):
  72. results.append({'suggestion': extract_text(suggestion)})
  73. return results