Selaa lähdekoodia

Create yahoo_news.py

Thomas Pointhuber 11 vuotta sitten
vanhempi
commit
f1404ce72d
1 muutettua tiedostoa jossa 51 lisäystä ja 0 poistoa
  1. 51
    0
      searx/engines/yahoo_news.py

+ 51
- 0
searx/engines/yahoo_news.py Näytä tiedosto

@@ -0,0 +1,51 @@
1
+#!/usr/bin/env python
2
+
3
+from urllib import urlencode
4
+from urlparse import unquote
5
+from lxml import html
6
+from searx.engines.xpath import extract_text, extract_url
7
+
8
+categories = ['news']
9
+search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}'
10
+results_xpath = '//div[@class="res"]'
11
+url_xpath = './/h3/a/@href'
12
+title_xpath = './/h3/a'
13
+content_xpath = './/div[@class="abstr"]'
14
+suggestion_xpath = '//div[@id="satat"]//a'
15
+
16
+paging = True
17
+
18
+
19
+def request(query, params):
20
+    offset = (params['pageno'] - 1) * 10 + 1
21
+    if params['language'] == 'all':
22
+        language = 'en'
23
+    else:
24
+        language = params['language'].split('_')[0]
25
+    params['url'] = search_url.format(offset=offset,
26
+                                      query=urlencode({'p': query}))
27
+    params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
28
+        .format(lang=language)
29
+    return params
30
+
31
+
32
+def response(resp):
33
+    results = []
34
+    dom = html.fromstring(resp.text)
35
+
36
+    for result in dom.xpath(results_xpath):
37
+        url_string = extract_url(result.xpath(url_xpath), search_url)
38
+        start = url_string.find('/RU=')+4
39
+        end = url_string.rfind('/RS')
40
+        url = unquote(url_string[start:end])
41
+        title = extract_text(result.xpath(title_xpath)[0])
42
+        content = extract_text(result.xpath(content_xpath)[0])
43
+        results.append({'url': url, 'title': title, 'content': content})
44
+
45
+    if not suggestion_xpath:
46
+        return results
47
+
48
+    for suggestion in dom.xpath(suggestion_xpath):
49
+        results.append({'suggestion': extract_text(suggestion)})
50
+
51
+    return results