startpage.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 (probably the parameter qid), require storing of qid's between mulitble search-calls
  19. #paging = False
  20. language_support = True
  21. # search-url
  22. base_url = 'https://startpage.com/'
  23. search_url = base_url + 'do/search'
  24. # specific xpath variables
  25. # ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
  26. # not ads: div[@class="result"] are the direct childs of div[@id="results"]
  27. results_xpath = '//div[@class="result"]'
  28. link_xpath = './/h3/a'
  29. # do search-request
  30. def request(query, params):
  31. offset = (params['pageno'] - 1) * 10
  32. query = urlencode({'q': query})[2:]
  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_' + params['language'].split('_')[0]
  40. return params
  41. # get response from search-request
  42. def response(resp):
  43. results = []
  44. dom = html.fromstring(resp.content)
  45. # parse results
  46. for result in dom.xpath(results_xpath):
  47. link = result.xpath(link_xpath)[0]
  48. url = link.attrib.get('href')
  49. title = escape(link.text_content())
  50. # block google-ad url's
  51. if re.match("^http(s|)://www.google.[a-z]+/aclk.*$", url):
  52. continue
  53. if result.xpath('./p[@class="desc"]'):
  54. content = escape(result.xpath('./p[@class="desc"]')[0].text_content())
  55. else:
  56. content = ''
  57. # append result
  58. results.append({'url': url,
  59. 'title': title,
  60. 'content': content})
  61. # return results
  62. return results