@@ -1,64 +1,99 @@
1−#!/usr/bin/env python
1+## Yahoo (Web)
3+# @website https://search.yahoo.com/web
4+# @provide-api yes (https://developer.yahoo.com/boss/search/), $0.80/1000 queries
6+# @using-api no (because pricing)
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, content, suggestion
3from urllib import urlencode
11from urllib import urlencode
4from urlparse import unquote
12from urlparse import unquote
5from lxml import html
13from lxml import html
6from searx.engines.xpath import extract_text, extract_url
14from searx.engines.xpath import extract_text, extract_url
16+# engine dependent config
8categories = ['general']
17categories = ['general']
19+language_support = True
9−search_url = 'http://search.yahoo.com/search?{query}&b={offset}'
22+search_url = 'https://search.yahoo.com/search?{query}&b={offset}&fl=1&vl=lang_{lang}'
24+# specific xpath variables
10results_xpath = '//div[@class="res"]'
25results_xpath = '//div[@class="res"]'
11url_xpath = './/h3/a/@href'
26url_xpath = './/h3/a/@href'
12title_xpath = './/h3/a'
27title_xpath = './/h3/a'
13content_xpath = './/div[@class="abstr"]'
28content_xpath = './/div[@class="abstr"]'
14suggestion_xpath = '//div[@id="satat"]//a'
29suggestion_xpath = '//div[@id="satat"]//a'
32+# remove yahoo-specific tracking-url
19def parse_url(url_string):
33def parse_url(url_string):
20 endings = ['/RS', '/RK']
34 endings = ['/RS', '/RK']
21 endpositions = []
35 endpositions = []
22 start = url_string.find('http', url_string.find('/RU=')+1)
36 start = url_string.find('http', url_string.find('/RU=')+1)
23 for ending in endings:
38 for ending in endings:
24 endpos = url_string.rfind(ending)
39 endpos = url_string.rfind(ending)
25 if endpos > -1:
40 if endpos > -1:
26 endpositions.append(endpos)
41 endpositions.append(endpos)
28 end = min(endpositions)
43 end = min(endpositions)
29 return unquote(url_string[start:end])
45 return unquote(url_string[start:end])
32def request(query, params):
49def request(query, params):
33 offset = (params['pageno'] - 1) * 10 + 1
50 offset = (params['pageno'] - 1) * 10 + 1
34 if params['language'] == 'all':
52 if params['language'] == 'all':
35 language = 'en'
53 language = 'en'
37 language = params['language'].split('_')[0]
55 language = params['language'].split('_')[0]
38 params['url'] = search_url.format(offset=offset,
57 params['url'] = search_url.format(offset=offset,
39− query=urlencode({'p': query}))
58+ query=urlencode({'p': query}),
40 params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
62 params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
41 .format(lang=language)
63 .format(lang=language)
42 return params
65 return params
68+# get response from search-request
45def response(resp):
69def response(resp):
46 results = []
70 results = []
47 dom = html.fromstring(resp.text)
72 dom = html.fromstring(resp.text)
49 for result in dom.xpath(results_xpath):
75 for result in dom.xpath(results_xpath):
51 url = parse_url(extract_url(result.xpath(url_xpath), search_url))
77 url = parse_url(extract_url(result.xpath(url_xpath), search_url))
52 title = extract_text(result.xpath(title_xpath)[0])
78 title = extract_text(result.xpath(title_xpath)[0])
55 content = extract_text(result.xpath(content_xpath)[0])
82 content = extract_text(result.xpath(content_xpath)[0])
56− results.append({'url': url, 'title': title, 'content': content})
85+ results.append({'url': url,
89+ # if no suggestion found, return results
58 if not suggestion_xpath:
90 if not suggestion_xpath:
59 return results
91 return results
61 for suggestion in dom.xpath(suggestion_xpath):
94 for suggestion in dom.xpath(suggestion_xpath):
62 results.append({'suggestion': extract_text(suggestion)})
96 results.append({'suggestion': extract_text(suggestion)})
64 return results
99 return results