startpage.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. # engine dependent config
  16. categories = ['general']
  17. # there is a mechanism to block "bot" search
  18. # (probably the parameter qid), require
  19. # storing of qid's between mulitble search-calls
  20. # paging = False
  21. language_support = True
  22. # search-url
  23. base_url = 'https://startpage.com/'
  24. search_url = base_url + 'do/search'
  25. # specific xpath variables
  26. # ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
  27. # not ads: div[@class="result"] are the direct childs of div[@id="results"]
  28. results_xpath = '//div[@class="result"]'
  29. link_xpath = './/h3/a'
  30. # do search-request
  31. def request(query, params):
  32. offset = (params['pageno'] - 1) * 10
  33. params['url'] = search_url
  34. params['method'] = 'POST'
  35. params['data'] = {'query': query,
  36. 'startat': offset}
  37. # set language if specified
  38. if params['language'] != 'all':
  39. params['data']['with_language'] = ('lang_' +
  40. 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. try:
  54. title = escape(link.text_content())
  55. except UnicodeDecodeError:
  56. continue
  57. # block google-ad url's
  58. if re.match("^http(s|)://www.google.[a-z]+/aclk.*$", url):
  59. continue
  60. if result.xpath('./p[@class="desc"]'):
  61. content = escape(result.xpath('./p[@class="desc"]')[0]
  62. .text_content())
  63. else:
  64. content = ''
  65. # append result
  66. results.append({'url': url,
  67. 'title': title,
  68. 'content': content})
  69. # return results
  70. return results