|
@@ -0,0 +1,51 @@
|
|
1
|
+from urllib import urlencode
|
|
2
|
+from cgi import escape
|
|
3
|
+from lxml import html
|
|
4
|
+
|
|
5
|
+categories = ['news']
|
|
6
|
+
|
|
7
|
+base_url = 'http://www.bing.com/'
|
|
8
|
+search_string = 'news/search?{query}&first={offset}'
|
|
9
|
+paging = True
|
|
10
|
+language_support = True
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+def request(query, params):
|
|
14
|
+ offset = (params['pageno'] - 1) * 10 + 1
|
|
15
|
+ if params['language'] == 'all':
|
|
16
|
+ language = 'en-US'
|
|
17
|
+ else:
|
|
18
|
+ language = params['language'].replace('_', '-')
|
|
19
|
+ search_path = search_string.format(
|
|
20
|
+ query=urlencode({'q': query, 'setmkt': language}),
|
|
21
|
+ offset=offset)
|
|
22
|
+
|
|
23
|
+ params['cookies']['SRCHHPGUSR'] = \
|
|
24
|
+ 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0]
|
|
25
|
+ #if params['category'] == 'images':
|
|
26
|
+ # params['url'] = base_url + 'images/' + search_path
|
|
27
|
+ params['url'] = base_url + search_path
|
|
28
|
+ return params
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+def response(resp):
|
|
32
|
+ global base_url
|
|
33
|
+ results = []
|
|
34
|
+ dom = html.fromstring(resp.content)
|
|
35
|
+ for result in dom.xpath('//div[@class="sa_cc"]'):
|
|
36
|
+ link = result.xpath('.//h3/a')[0]
|
|
37
|
+ url = link.attrib.get('href')
|
|
38
|
+ title = ' '.join(link.xpath('.//text()'))
|
|
39
|
+ content = escape(' '.join(result.xpath('.//p//text()')))
|
|
40
|
+ results.append({'url': url, 'title': title, 'content': content})
|
|
41
|
+
|
|
42
|
+ if results:
|
|
43
|
+ return results
|
|
44
|
+
|
|
45
|
+ for result in dom.xpath('//li[@class="b_algo"]'):
|
|
46
|
+ link = result.xpath('.//h2/a')[0]
|
|
47
|
+ url = link.attrib.get('href')
|
|
48
|
+ title = ' '.join(link.xpath('.//text()'))
|
|
49
|
+ content = escape(' '.join(result.xpath('.//p//text()')))
|
|
50
|
+ results.append({'url': url, 'title': title, 'content': content})
|
|
51
|
+ return results
|