@@ -0,0 +1,66 @@
3+# @website http://www.wolframalpha.com/
12+from urllib import urlencode
14+from searx.engines.xpath import extract_text
17+url = 'http://www.wolframalpha.com/'
18+search_url = url+'input/?{query}'
22+def request(query, params):
23+ params['url'] = search_url.format(query=urlencode({'i': query}))
28+# tries to find answer under the pattern given
29+def extract_answer(script_list, pattern):
32+ # get line that matches the pattern
33+ for script in script_list:
35+ line = re.search(pattern, script.text_content()).group(1)
36+ except AttributeError:
39+ # extract answer from json
40+ answer = line[line.find('{') : line.rfind('}')+1]
41+ answer = json.loads(answer.encode('unicode-escape'))
42+ answer = answer['stringified'].decode('unicode-escape')
47+# get response from search-request
50+ dom = html.fromstring(resp.text)
52+ # the answer is inside a js script
53+ scripts = dom.xpath('//script')
57+ # answer can be located in different 'pods', although by default it should be in pod_0200
58+ answer = extract_answer(scripts, 'pod_0200\.push(.*)\n')
60+ answer = extract_answer(scripts, 'pod_0100\.push(.*)\n')
62+ results.append({'answer': answer})
64+ results.append({'answer': answer})