123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 urllib import urlencode
  13. from lxml import html
  14. from cgi import escape
  15. import re
  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. query = urlencode({'q': query})[2:]
  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_' +
  42. params['language'].split('_')[0])
  43. return params
  44. # get response from search-request
  45. def response(resp):
  46. results = []
  47. dom = html.fromstring(resp.content)
  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. title = escape(link.text_content())
  56. # block google-ad url's
  57. if re.match("^http(s|)://www.google.[a-z]+/aclk.*$", url):
  58. continue
  59. if result.xpath('./p[@class="desc"]'):
  60. content = escape(result.xpath('./p[@class="desc"]')[0]
  61. .text_content())
  62. else:
  63. content = ''
  64. # append result
  65. results.append({'url': url,
  66. 'title': title,
  67. 'content': content})
  68. # return results
  69. return results