| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | ## DuckDuckGo (Web) | 
					
						
							| 
									
										
										
										
											2014-12-07 16:37:56 +01:00
										 |  |  | # | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | # @website     https://duckduckgo.com/ | 
					
						
							| 
									
										
										
										
											2014-12-07 16:37:56 +01:00
										 |  |  | # @provide-api yes (https://duckduckgo.com/api), | 
					
						
							|  |  |  | #              but not all results from search-site | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | # @using-api   no | 
					
						
							|  |  |  | # @results     HTML (using search portal) | 
					
						
							|  |  |  | # @stable      no (HTML can change) | 
					
						
							|  |  |  | # @parse       url, title, content | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # @todo        rewrite to api | 
					
						
							| 
									
										
										
										
											2014-12-07 16:37:56 +01:00
										 |  |  | # @todo        language support | 
					
						
							|  |  |  | #              (the current used site does not support language-change) | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-23 23:55:37 +02:00
										 |  |  | from urllib import urlencode | 
					
						
							| 
									
										
										
										
											2014-03-21 16:33:17 +01:00
										 |  |  | from lxml.html import fromstring | 
					
						
							| 
									
										
										
										
											2013-11-09 18:39:20 +01:00
										 |  |  | from searx.utils import html_to_text | 
					
						
							| 
									
										
										
										
											2013-10-14 23:09:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | # engine dependent config | 
					
						
							|  |  |  | categories = ['general'] | 
					
						
							|  |  |  | paging = True | 
					
						
							| 
									
										
										
										
											2014-09-07 22:52:13 +02:00
										 |  |  | language_support = True | 
					
						
							| 
									
										
										
										
											2013-10-14 23:09:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | # search-url | 
					
						
							|  |  |  | url = 'https://duckduckgo.com/html?{query}&s={offset}' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # specific xpath variables | 
					
						
							|  |  |  | result_xpath = '//div[@class="results_links results_links_deep web-result"]'  # noqa | 
					
						
							|  |  |  | url_xpath = './/a[@class="large"]/@href' | 
					
						
							|  |  |  | title_xpath = './/a[@class="large"]//text()' | 
					
						
							|  |  |  | content_xpath = './/div[@class="snippet"]//text()' | 
					
						
							| 
									
										
										
										
											2014-03-29 16:38:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | # do search-request | 
					
						
							| 
									
										
										
										
											2013-10-14 23:09:13 +02:00
										 |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2014-01-30 01:03:19 +01:00
										 |  |  |     offset = (params['pageno'] - 1) * 30 | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-07 22:52:13 +02:00
										 |  |  |     if params['language'] == 'all': | 
					
						
							|  |  |  |         locale = 'en-us' | 
					
						
							|  |  |  |     else: | 
					
						
							| 
									
										
										
										
											2014-12-07 16:37:56 +01:00
										 |  |  |         locale = params['language'].replace('_', '-').lower() | 
					
						
							| 
									
										
										
										
											2014-09-07 22:52:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  |     params['url'] = url.format( | 
					
						
							| 
									
										
										
										
											2014-09-07 22:52:13 +02:00
										 |  |  |         query=urlencode({'q': query, 'kl': locale}), | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  |         offset=offset) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-14 23:09:13 +02:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | # get response from search-request | 
					
						
							| 
									
										
										
										
											2013-10-14 23:09:13 +02:00
										 |  |  | def response(resp): | 
					
						
							| 
									
										
										
										
											2013-10-15 19:11:43 +02:00
										 |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2014-03-21 16:33:17 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     doc = fromstring(resp.text) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 18:12:42 +02:00
										 |  |  |     # parse results | 
					
						
							| 
									
										
										
										
											2014-03-21 16:33:17 +01:00
										 |  |  |     for r in doc.xpath(result_xpath): | 
					
						
							| 
									
										
										
										
											2014-03-21 18:17:13 +01:00
										 |  |  |         try: | 
					
						
							|  |  |  |             res_url = r.xpath(url_xpath)[-1] | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-21 16:33:17 +01:00
										 |  |  |         if not res_url: | 
					
						
							| 
									
										
										
										
											2013-10-15 19:11:43 +02:00
										 |  |  |             continue | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-21 16:33:17 +01:00
										 |  |  |         title = html_to_text(''.join(r.xpath(title_xpath))) | 
					
						
							|  |  |  |         content = html_to_text(''.join(r.xpath(content_xpath))) | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # append result | 
					
						
							| 
									
										
										
										
											2014-03-21 16:33:17 +01:00
										 |  |  |         results.append({'title': title, | 
					
						
							|  |  |  |                         'content': content, | 
					
						
							|  |  |  |                         'url': res_url}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 17:14:57 +02:00
										 |  |  |     # return results | 
					
						
							| 
									
										
										
										
											2013-10-15 19:11:43 +02:00
										 |  |  |     return results |