startpage.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 cgi import escape
  14. import re
  15. from searx.engines.xpath import extract_text
  16. # engine dependent config
  17. categories = ['general']
  18. # there is a mechanism to block "bot" search
  19. # (probably the parameter qid), require
  20. # storing of qid's between mulitble search-calls
  21. # paging = False
  22. language_support = True
  23. # search-url
  24. base_url = 'https://startpage.com/'
  25. search_url = base_url + 'do/search'
  26. # specific xpath variables
  27. # ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
  28. # not ads: div[@class="result"] are the direct childs of div[@id="results"]
  29. results_xpath = '//div[@class="result"]'
  30. link_xpath = './/h3/a'
  31. # do search-request
  32. def request(query, params):
  33. offset = (params['pageno'] - 1) * 10
  34. params['url'] = search_url
  35. params['method'] = 'POST'
  36. params['data'] = {'query': query,
  37. 'startat': offset}
  38. # set language if specified
  39. if params['language'] != 'all':
  40. params['data']['with_language'] = ('lang_' + params['language'].split('_')[0])
  41. return params
  42. # get response from search-request
  43. def response(resp):
  44. results = []
  45. dom = html.fromstring(resp.content)
  46. # parse results
  47. for result in dom.xpath(results_xpath):
  48. links = result.xpath(link_xpath)
  49. if not links:
  50. continue
  51. link = links[0]
  52. url = link.attrib.get('href')
  53. # block google-ad url's
  54. if re.match("^http(s|)://www.google.[a-z]+/aclk.*$", url):
  55. continue
  56. title = escape(extract_text(link))
  57. if result.xpath('./p[@class="desc"]'):
  58. content = escape(extract_text(result.xpath('./p[@class="desc"]')))
  59. else:
  60. content = ''
  61. # append result
  62. results.append({'url': url,
  63. 'title': title,
  64. 'content': content})
  65. # return results
  66. return results