startpage.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Startpage (Web)
  2. #
  3. # @website https://startpage.com
  4. # @provide-api no (nothing found)
  5. #
  6. # @using-api no
  7. # @results HTML
  8. # @stable no (HTML can change)
  9. # @parse url, title, content
  10. #
  11. # @todo paging
  12. from lxml import html
  13. from dateutil import parser
  14. from datetime import datetime, timedelta
  15. import re
  16. from searx.engines.xpath import extract_text
  17. # engine dependent config
  18. categories = ['general']
  19. # there is a mechanism to block "bot" search
  20. # (probably the parameter qid), require
  21. # storing of qid's between mulitble search-calls
  22. # paging = False
  23. language_support = True
  24. # search-url
  25. base_url = 'https://startpage.com/'
  26. search_url = base_url + 'do/search'
  27. # specific xpath variables
  28. # ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
  29. # not ads: div[@class="result"] are the direct childs of div[@id="results"]
  30. results_xpath = '//li[contains(@class, "search-result") and contains(@class, "search-item")]'
  31. link_xpath = './/h3/a'
  32. content_xpath = './p[@class="search-item__body"]'
  33. # do search-request
  34. def request(query, params):
  35. offset = (params['pageno'] - 1) * 10
  36. params['url'] = search_url
  37. params['method'] = 'POST'
  38. params['data'] = {'query': query,
  39. 'startat': offset}
  40. # set language if specified
  41. if params['language'] != 'all':
  42. params['data']['with_language'] = ('lang_' + params['language'].split('-')[0])
  43. return params
  44. # get response from search-request
  45. def response(resp):
  46. results = []
  47. dom = html.fromstring(resp.text)
  48. # parse results
  49. for result in dom.xpath(results_xpath):
  50. links = result.xpath(link_xpath)
  51. if not links:
  52. continue
  53. link = links[0]
  54. url = link.attrib.get('href')
  55. # block google-ad url's
  56. if re.match(r"^http(s|)://(www\.)?google\.[a-z]+/aclk.*$", url):
  57. continue
  58. # block startpage search url's
  59. if re.match(r"^http(s|)://(www\.)?startpage\.com/do/search\?.*$", url):
  60. continue
  61. title = extract_text(link)
  62. if result.xpath(content_xpath):
  63. content = extract_text(result.xpath(content_xpath))
  64. else:
  65. content = ''
  66. published_date = None
  67. # check if search result starts with something like: "2 Sep 2014 ... "
  68. if re.match(r"^([1-9]|[1-2][0-9]|3[0-1]) [A-Z][a-z]{2} [0-9]{4} \.\.\. ", content):
  69. date_pos = content.find('...') + 4
  70. date_string = content[0:date_pos - 5]
  71. published_date = parser.parse(date_string, dayfirst=True)
  72. # fix content string
  73. content = content[date_pos:]
  74. # check if search result starts with something like: "5 days ago ... "
  75. elif re.match(r"^[0-9]+ days? ago \.\.\. ", content):
  76. date_pos = content.find('...') + 4
  77. date_string = content[0:date_pos - 5]
  78. # calculate datetime
  79. published_date = datetime.now() - timedelta(days=int(re.match(r'\d+', date_string).group()))
  80. # fix content string
  81. content = content[date_pos:]
  82. if published_date:
  83. # append result
  84. results.append({'url': url,
  85. 'title': title,
  86. 'content': content,
  87. 'publishedDate': published_date})
  88. else:
  89. # append result
  90. results.append({'url': url,
  91. 'title': title,
  92. 'content': content})
  93. # return results
  94. return results