on master help sign in
[enh] google scholar - python implementation of the engine

The old xpath configuration for google scholar did not work and is replaced by a python implementation. Signed-off-by: Markus Heiser <markus.heiser@darmarit.de>

commitd48e2e7b0b31e03451105cd6f8714f1531dfded8 authorMarkus Heiser <markus.heiser@darmarit.de> dateMon 1 Mar 2021, 15:02 (6 years ago) parente2fb500
4 files changed+300−19
Makefile+2−1 searx/data/engines_languages.json+140 searx/engines/google_scholar.py+157 searx/settings.yml+1−18
Makefile+2−1
@@ -195,7 +195,8 @@ PYLINT_FILES=\
195195 searx/engines/google_videos.py \
196196 searx/engines/google_images.py \
197197 searx/engines/mediathekviewweb.py \
198− utils/fetch_external_bangs.py
198+ utils/fetch_external_bangs.py \
199+ searx/engines/google_scholar.py
199200
200201test.pylint: pyenvinstall
201202 $(call cmd,pylint,$(PYLINT_FILES))
searx/data/engines_languages.json+140
@@ -25176,6 +25176,146 @@
2517625176 "name": "\u4e2d\u6587 (\u7e41\u9ad4)"
2517725177 }
2517825178 },
25179+ "google scholar": {
25180+ "af": {
25181+ "name": "Afrikaans"
25182+ },
25183+ "ar": {
25184+ "name": "\u0627\u0644\u0639\u0631\u0628\u064a\u0629"
25185+ },
25186+ "be": {
25187+ "name": "\u0431\u0435\u043b\u0430\u0440\u0443\u0441\u043a\u0430\u044f"
25188+ },
25189+ "bg": {
25190+ "name": "\u0431\u044a\u043b\u0433\u0430\u0440\u0441\u043a\u0438"
25191+ },
25192+ "ca": {
25193+ "name": "catal\u00e0"
25194+ },
25195+ "cs": {
25196+ "name": "\u010de\u0161tina"
25197+ },
25198+ "da": {
25199+ "name": "dansk"
25200+ },
25201+ "de": {
25202+ "name": "Deutsch"
25203+ },
25204+ "el": {
25205+ "name": "\u0395\u03bb\u03bb\u03b7\u03bd\u03b9\u03ba\u03ac"
25206+ },
25207+ "en": {
25208+ "name": "English"
25209+ },
25210+ "eo": {
25211+ "name": "esperanto"
25212+ },
25213+ "es": {
25214+ "name": "espa\u00f1ol"
25215+ },
25216+ "et": {
25217+ "name": "eesti"
25218+ },
25219+ "fa": {
25220+ "name": "\u0641\u0627\u0631\u0633\u06cc"
25221+ },
25222+ "fi": {
25223+ "name": "suomi"
25224+ },
25225+ "fr": {
25226+ "name": "fran\u00e7ais"
25227+ },
25228+ "hi": {
25229+ "name": "\u0939\u093f\u0928\u094d\u0926\u0940"
25230+ },
25231+ "hr": {
25232+ "name": "hrvatski"
25233+ },
25234+ "hu": {
25235+ "name": "magyar"
25236+ },
25237+ "hy": {
25238+ "name": "\u0570\u0561\u0575\u0565\u0580\u0565\u0576"
25239+ },
25240+ "id": {
25241+ "name": "Indonesia"
25242+ },
25243+ "is": {
25244+ "name": "\u00edslenska"
25245+ },
25246+ "it": {
25247+ "name": "italiano"
25248+ },
25249+ "iw": {
25250+ "name": "\u05e2\u05d1\u05e8\u05d9\u05ea"
25251+ },
25252+ "ja": {
25253+ "name": "\u65e5\u672c\u8a9e"
25254+ },
25255+ "ko": {
25256+ "name": "\ud55c\uad6d\uc5b4"
25257+ },
25258+ "lt": {
25259+ "name": "lietuvi\u0173"
25260+ },
25261+ "lv": {
25262+ "name": "latvie\u0161u"
25263+ },
25264+ "nl": {
25265+ "name": "Nederlands"
25266+ },
25267+ "no": {
25268+ "name": "norsk"
25269+ },
25270+ "pl": {
25271+ "name": "polski"
25272+ },
25273+ "pt": {
25274+ "name": "portugu\u00eas"
25275+ },
25276+ "ro": {
25277+ "name": "rom\u00e2n\u0103"
25278+ },
25279+ "ru": {
25280+ "name": "\u0440\u0443\u0441\u0441\u043a\u0438\u0439"
25281+ },
25282+ "sk": {
25283+ "name": "sloven\u010dina"
25284+ },
25285+ "sl": {
25286+ "name": "sloven\u0161\u010dina"
25287+ },
25288+ "sr": {
25289+ "name": "\u0441\u0440\u043f\u0441\u043a\u0438"
25290+ },
25291+ "sv": {
25292+ "name": "svenska"
25293+ },
25294+ "sw": {
25295+ "name": "Kiswahili"
25296+ },
25297+ "th": {
25298+ "name": "\u0e44\u0e17\u0e22"
25299+ },
25300+ "tl": {
25301+ "name": "Filipino"
25302+ },
25303+ "tr": {
25304+ "name": "T\u00fcrk\u00e7e"
25305+ },
25306+ "uk": {
25307+ "name": "\u0443\u043a\u0440\u0430\u0457\u043d\u0441\u044c\u043a\u0430"
25308+ },
25309+ "vi": {
25310+ "name": "Ti\u1ebfng Vi\u1ec7t"
25311+ },
25312+ "zh-CN": {
25313+ "name": "\u4e2d\u6587 (\u7b80\u4f53)"
25314+ },
25315+ "zh-TW": {
25316+ "name": "\u4e2d\u6587 (\u7e41\u9ad4)"
25317+ }
25318+ },
2517925319 "google videos": {
2518025320 "af": {
2518125321 "name": "Afrikaans"
searx/engines/google_scholar.pynew file+157
@@ -0,0 +1,157 @@
1+# SPDX-License-Identifier: AGPL-3.0-or-later
2+"""Google (Scholar)
3+
4+For detailed description of the *REST-full* API see: `Query Parameter
5+Definitions`_.
6+
7+.. _Query Parameter Definitions:
8+ https://developers.google.com/custom-search/docs/xml_results#WebSearch_Query_Parameter_Definitions
9+"""
10+
11+# pylint: disable=invalid-name, missing-function-docstring
12+
13+from urllib.parse import urlencode
14+from datetime import datetime
15+from lxml import html
16+from searx import logger
17+
18+from searx.utils import (
19+ eval_xpath,
20+ eval_xpath_list,
21+ extract_text,
22+)
23+
24+from searx.engines.google import (
25+ get_lang_info,
26+ time_range_dict,
27+ detect_google_sorry,
28+)
29+
30+# pylint: disable=unused-import
31+from searx.engines.google import (
32+ supported_languages_url,
33+ _fetch_supported_languages,
34+)
35+# pylint: enable=unused-import
36+
37+# about
38+about = {
39+ "website": 'https://scholar.google.com',
40+ "wikidata_id": 'Q494817',
41+ "official_api_documentation": 'https://developers.google.com/custom-search',
42+ "use_official_api": False,
43+ "require_api_key": False,
44+ "results": 'HTML',
45+}
46+
47+# engine dependent config
48+categories = ['science']
49+paging = True
50+language_support = True
51+use_locale_domain = True
52+time_range_support = True
53+safesearch = False
54+
55+logger = logger.getChild('google scholar')
56+
57+def time_range_url(params):
58+ """Returns a URL query component for a google-Scholar time range based on
59+ ``params['time_range']``. Google-Scholar does only support ranges in years.
60+ To have any effect, all the Searx ranges (*day*, *week*, *month*, *year*)
61+ are mapped to *year*. If no range is set, an empty string is returned.
62+ Example::
63+
64+ &as_ylo=2019
65+ """
66+ # as_ylo=2016&as_yhi=2019
67+ ret_val = ''
68+ if params['time_range'] in time_range_dict:
69+ ret_val= urlencode({'as_ylo': datetime.now().year -1 })
70+ return '&' + ret_val
71+
72+
73+def request(query, params):
74+ """Google-Scholar search request"""
75+
76+ offset = (params['pageno'] - 1) * 10
77+ lang_info = get_lang_info(
78+ # pylint: disable=undefined-variable
79+
80+
81+ # params, {}, language_aliases
82+
83+ params, supported_languages, language_aliases
84+ )
85+ # subdomain is: scholar.google.xy
86+ lang_info['subdomain'] = lang_info['subdomain'].replace("www.", "scholar.")
87+
88+ query_url = 'https://'+ lang_info['subdomain'] + '/scholar' + "?" + urlencode({
89+ 'q': query,
90+ 'hl': lang_info['hl'],
91+ 'lr': lang_info['lr'],
92+ 'ie': "utf8",
93+ 'oe': "utf8",
94+ 'start' : offset,
95+ })
96+
97+ query_url += time_range_url(params)
98+
99+ logger.debug("query_url --> %s", query_url)
100+ params['url'] = query_url
101+
102+ logger.debug("HTTP header Accept-Language --> %s", lang_info['Accept-Language'])
103+ params['headers']['Accept-Language'] = lang_info['Accept-Language']
104+ params['headers']['Accept'] = (
105+ 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
106+ )
107+
108+ #params['google_subdomain'] = subdomain
109+ return params
110+
111+def response(resp):
112+ """Get response from google's search request"""
113+ results = []
114+
115+ detect_google_sorry(resp)
116+
117+ # which subdomain ?
118+ # subdomain = resp.search_params.get('google_subdomain')
119+
120+ # convert the text to dom
121+ dom = html.fromstring(resp.text)
122+
123+ # parse results
124+ for result in eval_xpath_list(dom, '//div[@class="gs_ri"]'):
125+
126+ title = extract_text(eval_xpath(result, './h3[1]//a'))
127+
128+ if not title:
129+ # this is a [ZITATION] block
130+ continue
131+
132+ url = eval_xpath(result, './h3[1]//a/@href')[0]
133+ content = extract_text(eval_xpath(result, './div[@class="gs_rs"]')) or ''
134+
135+ pub_info = extract_text(eval_xpath(result, './div[@class="gs_a"]'))
136+ if pub_info:
137+ content += "[%s]" % pub_info
138+
139+ pub_type = extract_text(eval_xpath(result, './/span[@class="gs_ct1"]'))
140+ if pub_type:
141+ title = title + " " + pub_type
142+
143+ results.append({
144+ 'url': url,
145+ 'title': title,
146+ 'content': content,
147+ })
148+
149+ # parse suggestion
150+ for suggestion in eval_xpath(dom, '//div[contains(@class, "gs_qsuggest_wrap")]//li//a'):
151+ # append suggestion
152+ results.append({'suggestion': extract_text(suggestion)})
153+
154+ for correction in eval_xpath(dom, '//div[@class="gs_r gs_pda"]/a'):
155+ results.append({'correction': extract_text(correction)})
156+
157+ return results
searx/settings.yml+1−18
@@ -535,25 +535,8 @@ engines:
535535 # android: *test_android
536536
537537 - name : google scholar
538− engine : xpath
539− paging : True
540− search_url : https://scholar.google.com/scholar?start={pageno}&q={query}&hl=en&as_sdt=0,5&as_vis=1
541− results_xpath : //div[contains(@class, "gs_r")]/div[@class="gs_ri"]
542− url_xpath : .//h3/a/@href
543− title_xpath : .//h3/a
544− content_xpath : .//div[@class="gs_rs"]
545− suggestion_xpath : //div[@id="gs_res_ccl_top"]//a/b
546− page_size : 10
547− first_page_num : 0
548− categories : science
538+ engine : google_scholar
549539 shortcut : gos
550− about:
551− website: https://scholar.google.com/
552− wikidata_id: Q494817
553− official_api_documentation:
554− use_official_api: false
555− require_api_key: false
556− results: HTML
557540
558541 - name : google play apps
559542 engine : xpath