google.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. if (parsed_url.netloc == google_hostname
  71. and parsed_url.path == images_path):
  72. # images result
  73. results = results + parse_images(result)
  74. else:
  75. # normal result
  76. content = extract_text(result.xpath(content_xpath)[0])
  77. # append result
  78. results.append({'url': url,
  79. 'title': title,
  80. 'content': content})
  81. except:
  82. continue
  83. # parse suggestion
  84. for suggestion in dom.xpath(suggestion_xpath):
  85. # append suggestion
  86. results.append({'suggestion': extract_text(suggestion)})
  87. # return results
  88. return results
  89. def parse_images(result):
  90. results = []
  91. for image in result.xpath(images_xpath):
  92. url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
  93. img_src = extract_text(image.xpath(image_img_src_xpath)[0])
  94. # append result
  95. results.append({'url': url,
  96. 'title': '',
  97. 'content': '',
  98. 'img_src': img_src,
  99. 'template': 'images.html'})
  100. return results