startpage.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. supported_languages = ["af", "de", "ar", "hy", "be", "bg", "ca", "cs", "zh-CN", "zh-TW",
  25. "ko", "hr", "da", "sk", "sl", "es", "eo", "et", "fi", "fr",
  26. "el", "iw", "hi", "nl", "hu", "id", "en", "is", "it", "ja",
  27. "lv", "lt", "no", "fa", "pl", "pt", "ro", "ru", "sr", "sw",
  28. "sv", "tl", "th", "tr", "uk", "vi"]
  29. # search-url
  30. base_url = 'https://startpage.com/'
  31. search_url = base_url + 'do/search'
  32. # specific xpath variables
  33. # ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
  34. # not ads: div[@class="result"] are the direct childs of div[@id="results"]
  35. results_xpath = '//div[@class="result"]'
  36. link_xpath = './/h3/a'
  37. # do search-request
  38. def request(query, params):
  39. offset = (params['pageno'] - 1) * 10
  40. params['url'] = search_url
  41. params['method'] = 'POST'
  42. params['data'] = {'query': query,
  43. 'startat': offset}
  44. # set language if specified
  45. if params['language'] != 'all':
  46. params['data']['with_language'] = ('lang_' + params['language'].split('-')[0])
  47. return params
  48. # get response from search-request
  49. def response(resp):
  50. results = []
  51. dom = html.fromstring(resp.content)
  52. # parse results
  53. for result in dom.xpath(results_xpath):
  54. links = result.xpath(link_xpath)
  55. if not links:
  56. continue
  57. link = links[0]
  58. url = link.attrib.get('href')
  59. # block google-ad url's
  60. if re.match(r"^http(s|)://(www\.)?google\.[a-z]+/aclk.*$", url):
  61. continue
  62. # block startpage search url's
  63. if re.match(r"^http(s|)://(www\.)?startpage\.com/do/search\?.*$", url):
  64. continue
  65. # block ixquick search url's
  66. if re.match(r"^http(s|)://(www\.)?ixquick\.com/do/search\?.*$", url):
  67. continue
  68. title = extract_text(link)
  69. if result.xpath('./p[@class="desc clk"]'):
  70. content = extract_text(result.xpath('./p[@class="desc clk"]'))
  71. else:
  72. content = ''
  73. published_date = None
  74. # check if search result starts with something like: "2 Sep 2014 ... "
  75. if re.match(r"^([1-9]|[1-2][0-9]|3[0-1]) [A-Z][a-z]{2} [0-9]{4} \.\.\. ", content):
  76. date_pos = content.find('...') + 4
  77. date_string = content[0:date_pos - 5]
  78. published_date = parser.parse(date_string, dayfirst=True)
  79. # fix content string
  80. content = content[date_pos:]
  81. # check if search result starts with something like: "5 days ago ... "
  82. elif re.match(r"^[0-9]+ days? ago \.\.\. ", content):
  83. date_pos = content.find('...') + 4
  84. date_string = content[0:date_pos - 5]
  85. # calculate datetime
  86. published_date = datetime.now() - timedelta(days=int(re.match(r'\d+', date_string).group()))
  87. # fix content string
  88. content = content[date_pos:]
  89. if published_date:
  90. # append result
  91. results.append({'url': url,
  92. 'title': title,
  93. 'content': content,
  94. 'publishedDate': published_date})
  95. else:
  96. # append result
  97. results.append({'url': url,
  98. 'title': title,
  99. 'content': content})
  100. # return results
  101. return results