瀏覽代碼

[enh] yahoo engine added

asciimoo 11 年之前
父節點
當前提交
642986c2e7
共有 1 個檔案被更改,包括 42 行新增0 行删除
  1. 42
    0
      searx/engines/yahoo.py

+ 42
- 0
searx/engines/yahoo.py 查看文件

@@ -0,0 +1,42 @@
1
+#!/usr/bin/env python
2
+
3
+from urllib import urlencode
4
+from searx.engines.xpath import extract_text, extract_url
5
+from lxml import html
6
+
7
+categories = ['general']
8
+search_url = 'http://search.yahoo.com/search?{query}&b={offset}'
9
+results_xpath = '//div[@class="res"]'
10
+url_xpath = './/h3/a/@href'
11
+title_xpath = './/h3/a'
12
+content_xpath = './/div[@class="abstr"]'
13
+suggestion_xpath = '//div[@id="satat"]//a'
14
+
15
+paging = True
16
+
17
+
18
+def request(query, params):
19
+    offset = (params['pageno'] - 1) * 10 + 1
20
+    params['url'] = search_url.format(offset=offset,
21
+                                      query=urlencode({'p': query}))
22
+    print params['url']
23
+    return params
24
+
25
+
26
+def response(resp):
27
+    results = []
28
+    dom = html.fromstring(resp.text)
29
+
30
+    for result in dom.xpath(results_xpath):
31
+        url = extract_url(result.xpath(url_xpath), search_url)
32
+        title = extract_text(result.xpath(title_xpath)[0])
33
+        content = extract_text(result.xpath(content_xpath)[0])
34
+        results.append({'url': url, 'title': title, 'content': content})
35
+
36
+    if not suggestion_xpath:
37
+        return results
38
+
39
+    for suggestion in dom.xpath(suggestion_xpath):
40
+        results.append({'suggestion': extract_text(suggestion)})
41
+
42
+    return results