| 
									
										
										
										
											2015-10-31 15:27:23 +01:00
										 |  |  | """
 | 
					
						
							|  |  |  |  Yahoo (Web) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  @website     https://yandex.ru/ | 
					
						
							|  |  |  |  @provide-api ? | 
					
						
							|  |  |  |  @using-api   no | 
					
						
							|  |  |  |  @results     HTML (using search portal) | 
					
						
							|  |  |  |  @stable      no (HTML can change) | 
					
						
							|  |  |  |  @parse       url, title, content | 
					
						
							|  |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-23 15:20:36 +01:00
										 |  |  | from cgi import escape | 
					
						
							| 
									
										
										
										
											2015-10-31 15:27:23 +01:00
										 |  |  | from urllib import urlencode | 
					
						
							|  |  |  | from lxml import html | 
					
						
							|  |  |  | from searx.search import logger | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | logger = logger.getChild('yandex engine') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # engine dependent config | 
					
						
							|  |  |  | categories = ['general'] | 
					
						
							|  |  |  | paging = True | 
					
						
							|  |  |  | language_support = True  # TODO | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 23:05:07 +01:00
										 |  |  | default_tld = 'com' | 
					
						
							|  |  |  | language_map = {'ru': 'ru', | 
					
						
							|  |  |  |                 'ua': 'uk', | 
					
						
							|  |  |  |                 'tr': 'com.tr'} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-31 15:27:23 +01:00
										 |  |  | # search-url | 
					
						
							| 
									
										
										
										
											2015-10-31 23:05:07 +01:00
										 |  |  | base_url = 'https://yandex.{tld}/' | 
					
						
							| 
									
										
										
										
											2015-10-31 15:27:23 +01:00
										 |  |  | search_url = 'search/?{query}&p={page}' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | results_xpath = '//div[@class="serp-item serp-item_plain_yes clearfix i-bem"]' | 
					
						
							|  |  |  | url_xpath = './/h2/a/@href' | 
					
						
							|  |  |  | title_xpath = './/h2/a//text()' | 
					
						
							|  |  |  | content_xpath = './/div[@class="serp-item__text"]//text()' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2015-10-31 23:05:07 +01:00
										 |  |  |     lang = params['language'].split('_')[0] | 
					
						
							|  |  |  |     host = base_url.format(tld=language_map.get(lang) or default_tld) | 
					
						
							| 
									
										
										
										
											2016-01-18 12:47:31 +01:00
										 |  |  |     params['url'] = host + search_url.format(page=params['pageno'] - 1, | 
					
						
							| 
									
										
										
										
											2015-10-31 23:05:07 +01:00
										 |  |  |                                              query=urlencode({'text': query})) | 
					
						
							| 
									
										
										
										
											2015-10-31 15:27:23 +01:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # get response from search-request | 
					
						
							|  |  |  | def response(resp): | 
					
						
							|  |  |  |     dom = html.fromstring(resp.text) | 
					
						
							|  |  |  |     results = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for result in dom.xpath(results_xpath): | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             res = {'url': result.xpath(url_xpath)[0], | 
					
						
							| 
									
										
										
										
											2016-01-23 15:20:36 +01:00
										 |  |  |                    'title': escape(''.join(result.xpath(title_xpath))), | 
					
						
							|  |  |  |                    'content': escape(''.join(result.xpath(content_xpath)))} | 
					
						
							| 
									
										
										
										
											2015-10-31 15:27:23 +01:00
										 |  |  |         except: | 
					
						
							|  |  |  |             logger.exception('yandex parse crash') | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         results.append(res) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return results |