google.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ## Google (Web)
  2. #
  3. # @website https://www.google.com
  4. # @provide-api yes (https://developers.google.com/custom-search/)
  5. #
  6. # @using-api no
  7. # @results HTML
  8. # @stable no (HTML can change)
  9. # @parse url, title, content, suggestion
  10. from urllib import urlencode
  11. from urlparse import unquote,urlparse,parse_qsl
  12. from lxml import html
  13. from searx.engines.xpath import extract_text, extract_url
  14. # engine dependent config
  15. categories = ['general']
  16. paging = True
  17. language_support = True
  18. # search-url
  19. google_hostname = 'www.google.com'
  20. search_path = '/search'
  21. redirect_path = '/url'
  22. images_path = '/images'
  23. search_url = 'https://' + google_hostname + search_path + '?{query}&start={offset}&gbv=1'
  24. # specific xpath variables
  25. results_xpath= '//li[@class="g"]'
  26. url_xpath = './/h3/a/@href'
  27. title_xpath = './/h3'
  28. content_xpath = './/span[@class="st"]'
  29. suggestion_xpath = '//p[@class="_Bmc"]'
  30. images_xpath = './/div/a'
  31. image_url_xpath = './@href'
  32. image_img_src_xpath = './img/@src'
  33. # remove google-specific tracking-url
  34. def parse_url(url_string):
  35. parsed_url = urlparse(url_string)
  36. if parsed_url.netloc in [google_hostname, ''] and parsed_url.path==redirect_path:
  37. query = dict(parse_qsl(parsed_url.query))
  38. return query['q']
  39. else:
  40. return url_string
  41. # do search-request
  42. def request(query, params):
  43. offset = (params['pageno'] - 1) * 10
  44. if params['language'] == 'all':
  45. language = 'en'
  46. else:
  47. language = params['language'].replace('_','-').lower()
  48. params['url'] = search_url.format(offset=offset,
  49. query=urlencode({'q': query}))
  50. params['headers']['Accept-Language'] = language
  51. return params
  52. # get response from search-request
  53. def response(resp):
  54. results = []
  55. dom = html.fromstring(resp.text)
  56. # parse results
  57. for result in dom.xpath(results_xpath):
  58. title = extract_text(result.xpath(title_xpath)[0])
  59. try:
  60. url = parse_url(extract_url(result.xpath(url_xpath), search_url))
  61. parsed_url = urlparse(url)
  62. if parsed_url.netloc==google_hostname and parsed_url.path==search_path:
  63. # remove the link to google news
  64. continue
  65. if parsed_url.netloc==google_hostname and parsed_url.path==images_path:
  66. # images result
  67. results = results + parse_images(result)
  68. else:
  69. # normal result
  70. content = extract_text(result.xpath(content_xpath)[0])
  71. # append result
  72. results.append({'url': url,
  73. 'title': title,
  74. 'content': content})
  75. except:
  76. continue
  77. # parse suggestion
  78. for suggestion in dom.xpath(suggestion_xpath):
  79. # append suggestion
  80. results.append({'suggestion': extract_text(suggestion)})
  81. # return results
  82. return results
  83. def parse_images(result):
  84. results = []
  85. for image in result.xpath(images_xpath):
  86. url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
  87. img_src = extract_text(image.xpath(image_img_src_xpath)[0])
  88. # append result
  89. results.append({'url': url,
  90. 'title': '',
  91. 'content': '',
  92. 'img_src': img_src,
  93. 'template': 'images.html'})
  94. return results