Browse Source

Google Instant Answer results

Nicholas Kegler 6 years ago
parent
commit
9a30820e57
2 changed files with 11 additions and 3 deletions
  1. 10
    2
      searx/engines/google.py
  2. 1
    1
      searx/templates/oscar/results.html

+ 10
- 2
searx/engines/google.py View File

219
     # convert the text to dom
219
     # convert the text to dom
220
     dom = html.fromstring(resp.text)
220
     dom = html.fromstring(resp.text)
221
 
221
 
222
-    instant_answer = dom.xpath('//div[@id="_vBb"]//text()')
222
+    instant_answer = dom.xpath('//div[contains(@id, "ires")]//div[contains(@class, "hp-xpdbox")]')
223
     if instant_answer:
223
     if instant_answer:
224
-        results.append({'answer': u' '.join(instant_answer)})
224
+        answer_re = r'(?P<prefix><a\s+href=")\/url\?q=(?P<url>[^"]+?)\&amp\;[^"]*(?P<suffix>"\s*>)'
225
+        answer_subst = "\\g<prefix>\\g<url>\\g<suffix>"
226
+        answer_html = ['<br>']
227
+        for element in instant_answer:
228
+            answer_html.append(etree.tostring(element, method="html"))
229
+        answer_str = u' '.join(answer_html)
230
+        answer_fixed = re.sub(answer_re, answer_subst, answer_str, 0, re.MULTILINE)
231
+        results.append({'answer': answer_fixed})
232
+
225
     try:
233
     try:
226
         results_num = int(dom.xpath('//div[@id="resultStats"]//text()')[0]
234
         results_num = int(dom.xpath('//div[@id="resultStats"]//text()')[0]
227
                           .split()[1].replace(',', ''))
235
                           .split()[1].replace(',', ''))

+ 1
- 1
searx/templates/oscar/results.html View File

31
             {% if answers %}
31
             {% if answers %}
32
             {% for answer in answers %}
32
             {% for answer in answers %}
33
             <div class="result well">
33
             <div class="result well">
34
-                <span>{{ answer }}</span>
34
+                <span>{{ answer|safe }}</span>
35
             </div>
35
             </div>
36
             {% endfor %}
36
             {% endfor %}
37
             {% endif %}
37
             {% endif %}