startpage.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. from searx.languages import language_codes
  18. # engine dependent config
  19. categories = ['general']
  20. # there is a mechanism to block "bot" search
  21. # (probably the parameter qid), require
  22. # storing of qid's between mulitble search-calls
  23. paging = True
  24. language_support = True
  25. # search-url
  26. base_url = 'https://startpage.com/'
  27. search_url = base_url + 'do/search'
  28. # specific xpath variables
  29. # ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
  30. # not ads: div[@class="result"] are the direct childs of div[@id="results"]
  31. results_xpath = '//div[@class="w-gl__result"]'
  32. link_xpath = './/a[@class="w-gl__result-title"]'
  33. content_xpath = './/p[@class="w-gl__description"]'
  34. # do search-request
  35. def request(query, params):
  36. params['url'] = search_url
  37. params['method'] = 'POST'
  38. params['data'] = {
  39. 'query': query,
  40. 'page': params['pageno'],
  41. 'cat': 'web',
  42. 'cmd': 'process_search',
  43. 'engine0': 'v1all',
  44. }
  45. # set language if specified
  46. if params['language'] != 'all':
  47. language = 'english'
  48. for lc, _, _, lang in language_codes:
  49. if lc == params['language']:
  50. language = lang
  51. params['data']['language'] = language
  52. params['data']['lui'] = language
  53. return params
  54. # get response from search-request
  55. def response(resp):
  56. results = []
  57. dom = html.fromstring(resp.text)
  58. # parse results
  59. for result in dom.xpath(results_xpath):
  60. links = result.xpath(link_xpath)
  61. if not links:
  62. continue
  63. link = links[0]
  64. url = link.attrib.get('href')
  65. # block google-ad url's
  66. if re.match(r"^http(s|)://(www\.)?google\.[a-z]+/aclk.*$", url):
  67. continue
  68. # block startpage search url's
  69. if re.match(r"^http(s|)://(www\.)?startpage\.com/do/search\?.*$", url):
  70. continue
  71. title = extract_text(link)
  72. if result.xpath(content_xpath):
  73. content = extract_text(result.xpath(content_xpath))
  74. else:
  75. content = ''
  76. published_date = None
  77. # check if search result starts with something like: "2 Sep 2014 ... "
  78. if re.match(r"^([1-9]|[1-2][0-9]|3[0-1]) [A-Z][a-z]{2} [0-9]{4} \.\.\. ", content):
  79. date_pos = content.find('...') + 4
  80. date_string = content[0:date_pos - 5]
  81. published_date = parser.parse(date_string, dayfirst=True)
  82. # fix content string
  83. content = content[date_pos:]
  84. # check if search result starts with something like: "5 days ago ... "
  85. elif re.match(r"^[0-9]+ days? ago \.\.\. ", content):
  86. date_pos = content.find('...') + 4
  87. date_string = content[0:date_pos - 5]
  88. # calculate datetime
  89. published_date = datetime.now() - timedelta(days=int(re.match(r'\d+', date_string).group()))
  90. # fix content string
  91. content = content[date_pos:]
  92. if published_date:
  93. # append result
  94. results.append({'url': url,
  95. 'title': title,
  96. 'content': content,
  97. 'publishedDate': published_date})
  98. else:
  99. # append result
  100. results.append({'url': url,
  101. 'title': title,
  102. 'content': content})
  103. # return results
  104. return results