on master help sign in
[mod] google: add "use_mobile_ui" parameter to use mobile endpoint.
8bf216eAlexandre Flament
searx/engines/google.py+24−11
@@ -33,6 +33,7 @@ categories = ['general']
33paging = True
33paging = True
34time_range_support = True
34time_range_support = True
35safesearch = True
35safesearch = True
36+use_mobile_ui = False
36supported_languages_url = 'https://www.google.com/preferences?#languages'
37supported_languages_url = 'https://www.google.com/preferences?#languages'
37
38
38# based on https://en.wikipedia.org/wiki/List_of_Google_domains and tests
39# based on https://en.wikipedia.org/wiki/List_of_Google_domains and tests
@@ -206,6 +207,13 @@ def request(query, params):
206 params, supported_languages, language_aliases, True
207 params, supported_languages, language_aliases, True
207 )
208 )
208
209
210+ additional_parameters = {}
211+ if use_mobile_ui:
212+ additional_parameters = {
213+ 'asearch': "arc",
214+ 'async': 'arc_id:srp_510,ffilt:all,ve_name:MoreResultsContainer,next_id:srp_5,use_ac:true,_id:arc-srp_510,_pms:qs,_fmt:pc' # pylint: disable=line-too-long
215+ }
216+
209 # https://www.google.de/search?q=corona&hl=de&lr=lang_de&start=0&tbs=qdr%3Ad&safe=medium
217 # https://www.google.de/search?q=corona&hl=de&lr=lang_de&start=0&tbs=qdr%3Ad&safe=medium
210 query_url = 'https://' + lang_info['subdomain'] + '/search' + "?" + urlencode({
218 query_url = 'https://' + lang_info['subdomain'] + '/search' + "?" + urlencode({
211 'q': query,
219 'q': query,
@@ -213,6 +221,7 @@ def request(query, params):
213 'ie': "utf8",
221 'ie': "utf8",
214 'oe': "utf8",
222 'oe': "utf8",
215 'start': offset,
223 'start': offset,
224+ **additional_parameters,
216 })
225 })
217
226
218 if params['time_range'] in time_range_dict:
227 if params['time_range'] in time_range_dict:
@@ -225,9 +234,12 @@ def request(query, params):
225
234
226 logger.debug("HTTP header Accept-Language --> %s", lang_info.get('Accept-Language'))
235 logger.debug("HTTP header Accept-Language --> %s", lang_info.get('Accept-Language'))
227 params['headers'].update(lang_info['headers'])
236 params['headers'].update(lang_info['headers'])
237+ if use_mobile_ui:
238+ params['headers']['Accept'] = '*/*'
239+ else:
228− params['headers']['Accept'] = (
240+ params['headers']['Accept'] = (
229− 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
241+ 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
230− )
242+ )
231
243
232 return params
244 return params
233
245
@@ -250,14 +262,15 @@ def response(resp):
250 logger.debug("did not find 'answer'")
262 logger.debug("did not find 'answer'")
251
263
252 # results --> number_of_results
264 # results --> number_of_results
265+ if not use_mobile_ui:
253− try:
266+ try:
254− _txt = eval_xpath_getindex(dom, '//div[@id="result-stats"]//text()', 0)
267+ _txt = eval_xpath_getindex(dom, '//div[@id="result-stats"]//text()', 0)
255− _digit = ''.join([n for n in _txt if n.isdigit()])
268+ _digit = ''.join([n for n in _txt if n.isdigit()])
256− number_of_results = int(_digit)
269+ number_of_results = int(_digit)
257− results.append({'number_of_results': number_of_results})
270+ results.append({'number_of_results': number_of_results})
258− except Exception as e: # pylint: disable=broad-except
271+ except Exception as e: # pylint: disable=broad-except
259− logger.debug("did not 'number_of_results'")
272+ logger.debug("did not 'number_of_results'")
260− logger.error(e, exc_info=True)
273+ logger.error(e, exc_info=True)
261
274
262 # parse results
275 # parse results
263 for result in eval_xpath_list(dom, results_xpath):
276 for result in eval_xpath_list(dom, results_xpath):
searx/settings.yml+1
@@ -551,6 +551,7 @@ engines:
551 - name : google
551 - name : google
552 engine : google
552 engine : google
553 shortcut : go
553 shortcut : go
554+ use_mobile_ui: false
554 # additional_tests:
555 # additional_tests:
555 # android: *test_android
556 # android: *test_android
556
557