on master help sign in
fix yahoo engines and add comments
commit03db970e6a81cdbf7e09bfd9d809f4aa8b07d5b3 authorThomas Pointhuber <thomas.pointhuber@gmx.at> dateMon 1 Sep 2014, 16:17 (12 years ago) parent90dcfc1
2 files changed+72−17
searx/engines/yahoo.py+41−6 searx/engines/yahoo_news.py+31−11
searx/engines/yahoo.py+41−6
@@ -1,64 +1,99 @@
1−#!/usr/bin/env python
1+## Yahoo (Web)
2+#
3+# @website https://search.yahoo.com/web
4+# @provide-api yes (https://developer.yahoo.com/boss/search/), $0.80/1000 queries
5+#
6+# @using-api no (because pricing)
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, content, suggestion
210
311from urllib import urlencode
412from urlparse import unquote
513from lxml import html
614from searx.engines.xpath import extract_text, extract_url
715
16+# engine dependent config
817categories = ['general']
9−search_url = 'http://search.yahoo.com/search?{query}&b={offset}'
18+paging = True
19+language_support = True
20+
21+# search-url
22+search_url = 'https://search.yahoo.com/search?{query}&b={offset}&fl=1&vl=lang_{lang}'
23+
24+# specific xpath variables
1025results_xpath = '//div[@class="res"]'
1126url_xpath = './/h3/a/@href'
1227title_xpath = './/h3/a'
1328content_xpath = './/div[@class="abstr"]'
1429suggestion_xpath = '//div[@id="satat"]//a'
1530
16−paging = True
17−
1831
32+# remove yahoo-specific tracking-url
1933def parse_url(url_string):
2034 endings = ['/RS', '/RK']
2135 endpositions = []
2236 start = url_string.find('http', url_string.find('/RU=')+1)
37+
2338 for ending in endings:
2439 endpos = url_string.rfind(ending)
2540 if endpos > -1:
2641 endpositions.append(endpos)
2742
2843 end = min(endpositions)
44+
2945 return unquote(url_string[start:end])
3046
3147
48+# do search-request
3249def request(query, params):
3350 offset = (params['pageno'] - 1) * 10 + 1
51+
3452 if params['language'] == 'all':
3553 language = 'en'
3654 else:
3755 language = params['language'].split('_')[0]
56+
3857 params['url'] = search_url.format(offset=offset,
39− query=urlencode({'p': query}))
58+ query=urlencode({'p': query}),
59+ lang=language)
60+
61+ # TODO required?
4062 params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
4163 .format(lang=language)
64+
4265 return params
4366
4467
68+# get response from search-request
4569def response(resp):
4670 results = []
71+
4772 dom = html.fromstring(resp.text)
4873
74+ # parse results
4975 for result in dom.xpath(results_xpath):
5076 try:
5177 url = parse_url(extract_url(result.xpath(url_xpath), search_url))
5278 title = extract_text(result.xpath(title_xpath)[0])
5379 except:
5480 continue
81+
5582 content = extract_text(result.xpath(content_xpath)[0])
56− results.append({'url': url, 'title': title, 'content': content})
5783
84+ # append result
85+ results.append({'url': url,
86+ 'title': title,
87+ 'content': content})
88+
89+ # if no suggestion found, return results
5890 if not suggestion_xpath:
5991 return results
6092
93+ # parse suggestion
6194 for suggestion in dom.xpath(suggestion_xpath):
95+ # append suggestion
6296 results.append({'suggestion': extract_text(suggestion)})
6397
98+ # return results
6499 return results
searx/engines/yahoo_news.py+31−11
@@ -1,4 +1,12 @@
1−#!/usr/bin/env python
1+## Yahoo (News)
2+#
3+# @website https://news.yahoo.com
4+# @provide-api yes (https://developer.yahoo.com/boss/search/), $0.80/1000 queries
5+#
6+# @using-api no (because pricing)
7+# @results HTML (using search portal)
8+# @stable no (HTML can change)
9+# @parse url, title, content, publishedDate
210
311from urllib import urlencode
412from lxml import html
@@ -8,8 +16,15 @@ from datetime import datetime, timedelta
816import re
917from dateutil import parser
1018
19+# engine dependent config
1120categories = ['news']
12−search_url = 'http://news.search.yahoo.com/search?{query}&b={offset}'
21+paging = True
22+language_support = True
23+
24+# search-url
25+search_url = 'https://news.search.yahoo.com/search?{query}&b={offset}&fl=1&vl=lang_{lang}'
26+
27+# specific xpath variables
1328results_xpath = '//div[@class="res"]'
1429url_xpath = './/h3/a/@href'
1530title_xpath = './/h3/a'
@@ -17,30 +32,39 @@ content_xpath = './/div[@class="abstr"]'
1732publishedDate_xpath = './/span[@class="timestamp"]'
1833suggestion_xpath = '//div[@id="satat"]//a'
1934
20−paging = True
21−
2235
36+# do search-request
2337def request(query, params):
2438 offset = (params['pageno'] - 1) * 10 + 1
39+
2540 if params['language'] == 'all':
2641 language = 'en'
2742 else:
2843 language = params['language'].split('_')[0]
44+
2945 params['url'] = search_url.format(offset=offset,
30− query=urlencode({'p': query}))
46+ query=urlencode({'p': query}),
47+ lang=language)
48+
49+ # TODO required?
3150 params['cookies']['sB'] = 'fl=1&vl=lang_{lang}&sh=1&rw=new&v=1'\
3251 .format(lang=language)
3352 return params
3453
3554
55+# get response from search-request
3656def response(resp):
3757 results = []
58+
3859 dom = html.fromstring(resp.text)
3960
61+ # parse results
4062 for result in dom.xpath(results_xpath):
4163 url = parse_url(extract_url(result.xpath(url_xpath), search_url))
4264 title = extract_text(result.xpath(title_xpath)[0])
4365 content = extract_text(result.xpath(content_xpath)[0])
66+
67+ # parse publishedDate
4468 publishedDate = extract_text(result.xpath(publishedDate_xpath)[0])
4569
4670 if re.match("^[0-9]+ minute(s|) ago$", publishedDate):
@@ -58,15 +82,11 @@ def response(resp):
5882 if publishedDate.year == 1900:
5983 publishedDate = publishedDate.replace(year=datetime.now().year)
6084
85+ # append result
6186 results.append({'url': url,
6287 'title': title,
6388 'content': content,
6489 'publishedDate': publishedDate})
6590
66− if not suggestion_xpath:
67− return results
68−
69− for suggestion in dom.xpath(suggestion_xpath):
70− results.append({'suggestion': extract_text(suggestion)})
71−
91+ # return results
7292 return results