瀏覽代碼

[enh] added google images engine

stef 11 年之前
父節點
當前提交
7c11c7056f
共有 1 個文件被更改,包括 28 次插入0 次删除
  1. 28
    0
      searx/engines/gimages.py

+ 28
- 0
searx/engines/gimages.py 查看文件

@@ -0,0 +1,28 @@
1
+#!/usr/bin/env python
2
+
3
+from urllib import quote
4
+from lxml import html
5
+from urlparse import urljoin
6
+
7
+categories = ['test']
8
+
9
+base_url = 'https://www.google.com/'
10
+search_url = base_url+'search?tbm=isch&hl=en&q='
11
+
12
+def request(query, params):
13
+    global search_url
14
+    query = quote(query.replace(' ', '+'), safe='+')
15
+    params['url'] = search_url + query
16
+    return params
17
+
18
+def response(resp):
19
+    global base_url
20
+    results = []
21
+    dom = html.fromstring(resp.text)
22
+    for result in dom.xpath('//table[@class="images_table"]//a'):
23
+        url = urljoin(base_url, result.attrib.get('href'))
24
+        img = result.xpath('.//img')[0]
25
+        title = ' '.join(result.xpath('..//text()'))
26
+        content = '<img src="%s" alt="%s" />' % (img.attrib.get('src', ''), title)
27
+        results.append({'url': url, 'title': title, 'content': content})
28
+    return results