@@ -14,7 +14,7 @@ from lxml import html, etree
14from searx.engines.xpath import extract_text, extract_url
14from searx.engines.xpath import extract_text, extract_url
15from searx import logger
15from searx import logger
16from searx.url_utils import urlencode, urlparse, parse_qsl
16from searx.url_utils import urlencode, urlparse, parse_qsl
17−from searx.utils import match_language
17+from searx.utils import match_language, eval_xpath
19logger = logger.getChild('google engine')
19logger = logger.getChild('google engine')
@@ -156,7 +156,7 @@ def parse_url(url_string, google_hostname):
157# returns extract_text on the first result selected by the xpath or None
157# returns extract_text on the first result selected by the xpath or None
158def extract_text_from_dom(result, xpath):
158def extract_text_from_dom(result, xpath):
159− r = result.xpath(xpath)
159+ r = eval_xpath(result, xpath)
160 if len(r) > 0:
160 if len(r) > 0:
161 return extract_text(r[0])
161 return extract_text(r[0])
162 return None
162 return None
@@ -227,21 +227,21 @@ def response(resp):
227 # convert the text to dom
227 # convert the text to dom
228 dom = html.fromstring(resp.text)
228 dom = html.fromstring(resp.text)
230− instant_answer = dom.xpath('//div[@id="_vBb"]//text()')
230+ instant_answer = eval_xpath(dom, '//div[@id="_vBb"]//text()')
231 if instant_answer:
231 if instant_answer:
232 results.append({'answer': u' '.join(instant_answer)})
232 results.append({'answer': u' '.join(instant_answer)})
234− results_num = int(dom.xpath('//div[@id="resultStats"]//text()')[0]
234+ results_num = int(eval_xpath(dom, '//div[@id="resultStats"]//text()')[0]
235 .split()[1].replace(',', ''))
235 .split()[1].replace(',', ''))
236 results.append({'number_of_results': results_num})
236 results.append({'number_of_results': results_num})
240 # parse results
240 # parse results
241− for result in dom.xpath(results_xpath):
241+ for result in eval_xpath(dom, results_xpath):
243− title = extract_text(result.xpath(title_xpath)[0])
243+ title = extract_text(eval_xpath(result, title_xpath)[0])
244− url = parse_url(extract_url(result.xpath(url_xpath), google_url), google_hostname)
244+ url = parse_url(extract_url(eval_xpath(result, url_xpath), google_url), google_hostname)
245 parsed_url = urlparse(url, google_hostname)
245 parsed_url = urlparse(url, google_hostname)
247 # map result
247 # map result
@@ -250,7 +250,7 @@ def response(resp):
251 # if parsed_url.path.startswith(maps_path) or parsed_url.netloc.startswith(map_hostname_start):
251 # if parsed_url.path.startswith(maps_path) or parsed_url.netloc.startswith(map_hostname_start):
252 # print "yooooo"*30
252 # print "yooooo"*30
253− # x = result.xpath(map_near)
253+ # x = eval_xpath(result, map_near)
254 # if len(x) > 0:
254 # if len(x) > 0:
255 # # map : near the location
255 # # map : near the location
256 # results = results + parse_map_near(parsed_url, x, google_hostname)
256 # results = results + parse_map_near(parsed_url, x, google_hostname)
@@ -287,11 +287,11 @@ def response(resp):
289 # parse suggestion
289 # parse suggestion
290− for suggestion in dom.xpath(suggestion_xpath):
290+ for suggestion in eval_xpath(dom, suggestion_xpath):
291 # append suggestion
291 # append suggestion
292 results.append({'suggestion': extract_text(suggestion)})
292 results.append({'suggestion': extract_text(suggestion)})
294− for correction in dom.xpath(spelling_suggestion_xpath):
294+ for correction in eval_xpath(dom, spelling_suggestion_xpath):
295 results.append({'correction': extract_text(correction)})
295 results.append({'correction': extract_text(correction)})
297 # return results
297 # return results
@@ -300,9 +300,9 @@ def response(resp):
301def parse_images(result, google_hostname):
301def parse_images(result, google_hostname):
302 results = []
302 results = []
303− for image in result.xpath(images_xpath):
303+ for image in eval_xpath(result, images_xpath):
304− url = parse_url(extract_text(image.xpath(image_url_xpath)[0]), google_hostname)
304+ url = parse_url(extract_text(eval_xpath(image, image_url_xpath)[0]), google_hostname)
305− img_src = extract_text(image.xpath(image_img_src_xpath)[0])
305+ img_src = extract_text(eval_xpath(image, image_img_src_xpath)[0])
307 # append result
307 # append result
308 results.append({'url': url,
308 results.append({'url': url,
@@ -389,10 +389,10 @@ def attributes_to_html(attributes):
389def _fetch_supported_languages(resp):
389def _fetch_supported_languages(resp):
390 supported_languages = {}
390 supported_languages = {}
391 dom = html.fromstring(resp.text)
391 dom = html.fromstring(resp.text)
392− options = dom.xpath('//*[@id="langSec"]//input[@name="lr"]')
392+ options = eval_xpath(dom, '//*[@id="langSec"]//input[@name="lr"]')
393 for option in options:
393 for option in options:
394− code = option.xpath('./@value')[0].split('_')[-1]
394+ code = eval_xpath(option, './@value')[0].split('_')[-1]
395− name = option.xpath('./@data-name')[0].title()
395+ name = eval_xpath(option, './@data-name')[0].title()
396 supported_languages[code] = {"name": name}
396 supported_languages[code] = {"name": name}
398 return supported_languages
398 return supported_languages