startpage.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 = '//div[@class="result"]'
  31. link_xpath = './/h3/a'
  32. # do search-request
  33. def request(query, params):
  34. offset = (params['pageno'] - 1) * 10
  35. params['url'] = search_url
  36. params['method'] = 'POST'
  37. params['data'] = {'query': query,
  38. 'startat': offset}
  39. # set language if specified
  40. if params['language'] != 'all':
  41. params['data']['with_language'] = ('lang_' + params['language'].split('-')[0])
  42. return params
  43. # get response from search-request
  44. def response(resp):
  45. results = []
  46. dom = html.fromstring(resp.text)
  47. # parse results
  48. for result in dom.xpath(results_xpath):
  49. links = result.xpath(link_xpath)
  50. if not links:
  51. continue
  52. link = links[0]
  53. url = link.attrib.get('href')
  54. # block google-ad url's
  55. if re.match(r"^http(s|)://(www\.)?google\.[a-z]+/aclk.*$", url):
  56. continue
  57. # block startpage search url's
  58. if re.match(r"^http(s|)://(www\.)?startpage\.com/do/search\?.*$", url):
  59. continue
  60. # block ixquick search url's
  61. if re.match(r"^http(s|)://(www\.)?ixquick\.com/do/search\?.*$", url):
  62. continue
  63. title = extract_text(link)
  64. if result.xpath('./p[@class="desc clk"]'):
  65. content = extract_text(result.xpath('./p[@class="desc clk"]'))
  66. else:
  67. content = ''
  68. published_date = None
  69. # check if search result starts with something like: "2 Sep 2014 ... "
  70. if re.match(r"^([1-9]|[1-2][0-9]|3[0-1]) [A-Z][a-z]{2} [0-9]{4} \.\.\. ", content):
  71. date_pos = content.find('...') + 4
  72. date_string = content[0:date_pos - 5]
  73. published_date = parser.parse(date_string, dayfirst=True)
  74. # fix content string
  75. content = content[date_pos:]
  76. # check if search result starts with something like: "5 days ago ... "
  77. elif re.match(r"^[0-9]+ days? ago \.\.\. ", content):
  78. date_pos = content.find('...') + 4
  79. date_string = content[0:date_pos - 5]
  80. # calculate datetime
  81. published_date = datetime.now() - timedelta(days=int(re.match(r'\d+', date_string).group()))
  82. # fix content string
  83. content = content[date_pos:]
  84. if published_date:
  85. # append result
  86. results.append({'url': url,
  87. 'title': title,
  88. 'content': content,
  89. 'publishedDate': published_date})
  90. else:
  91. # append result
  92. results.append({'url': url,
  93. 'title': title,
  94. 'content': content})
  95. # return results
  96. return results