google.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 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://' +
  24. google_hostname +
  25. search_path +
  26. '?{query}&start={offset}&gbv=1')
  27. # specific xpath variables
  28. results_xpath = '//li[@class="g"]'
  29. url_xpath = './/h3/a/@href'
  30. title_xpath = './/h3'
  31. content_xpath = './/span[@class="st"]'
  32. suggestion_xpath = '//p[@class="_Bmc"]'
  33. images_xpath = './/div/a'
  34. image_url_xpath = './@href'
  35. image_img_src_xpath = './img/@src'
  36. # remove google-specific tracking-url
  37. def parse_url(url_string):
  38. parsed_url = urlparse(url_string)
  39. if (parsed_url.netloc in [google_hostname, '']
  40. and parsed_url.path == redirect_path):
  41. query = dict(parse_qsl(parsed_url.query))
  42. return query['q']
  43. else:
  44. return url_string
  45. # do search-request
  46. def request(query, params):
  47. offset = (params['pageno'] - 1) * 10
  48. if params['language'] == 'all':
  49. language = 'en'
  50. else:
  51. language = params['language'].replace('_', '-').lower()
  52. params['url'] = search_url.format(offset=offset,
  53. query=urlencode({'q': query}))
  54. params['headers']['Accept-Language'] = language
  55. return params
  56. # get response from search-request
  57. def response(resp):
  58. results = []
  59. dom = html.fromstring(resp.text)
  60. # parse results
  61. for result in dom.xpath(results_xpath):
  62. title = extract_text(result.xpath(title_xpath)[0])
  63. try:
  64. url = parse_url(extract_url(result.xpath(url_xpath), search_url))
  65. parsed_url = urlparse(url)
  66. if (parsed_url.netloc == google_hostname
  67. and parsed_url.path == search_path):
  68. # remove the link to google news
  69. continue
  70. # images result
  71. if (parsed_url.netloc == google_hostname
  72. and parsed_url.path == images_path):
  73. # only thumbnail image provided,
  74. # so skipping image results
  75. # results = results + parse_images(result)
  76. pass
  77. else:
  78. # normal result
  79. content = extract_text(result.xpath(content_xpath)[0])
  80. # append result
  81. results.append({'url': url,
  82. 'title': title,
  83. 'content': content})
  84. except:
  85. continue
  86. # parse suggestion
  87. for suggestion in dom.xpath(suggestion_xpath):
  88. # append suggestion
  89. results.append({'suggestion': extract_text(suggestion)})
  90. # return results
  91. return results
  92. def parse_images(result):
  93. results = []
  94. for image in result.xpath(images_xpath):
  95. url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
  96. img_src = extract_text(image.xpath(image_img_src_xpath)[0])
  97. # append result
  98. results.append({'url': url,
  99. 'title': '',
  100. 'content': '',
  101. 'img_src': img_src,
  102. 'template': 'images.html'})
  103. return results