google.py 3.9KB

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