| 
									
										
										
										
											2015-05-02 15:45:17 +02:00
										 |  |  | """
 | 
					
						
							|  |  |  |  Google (News) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  |  @website     https://news.google.com | 
					
						
							|  |  |  |  @provide-api no | 
					
						
							| 
									
										
										
										
											2015-05-02 15:45:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  |  @using-api   no | 
					
						
							|  |  |  |  @results     HTML | 
					
						
							|  |  |  |  @stable      no | 
					
						
							| 
									
										
										
										
											2015-05-02 15:45:17 +02:00
										 |  |  |  @parse       url, title, content, publishedDate | 
					
						
							|  |  |  | """
 | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  | from lxml import html | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  | from urllib import urlencode | 
					
						
							| 
									
										
										
										
											2016-08-06 06:34:56 +02:00
										 |  |  | from json import loads | 
					
						
							| 
									
										
										
										
											2016-12-15 07:34:43 +01:00
										 |  |  | from searx.engines.google import _fetch_supported_languages, supported_languages_url | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  | # search-url | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  | categories = ['news'] | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  | paging = True | 
					
						
							|  |  |  | language_support = True | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  | safesearch = True | 
					
						
							|  |  |  | time_range_support = True | 
					
						
							|  |  |  | number_of_results = 10 | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  | search_url = 'https://www.google.com/search'\ | 
					
						
							|  |  |  |     '?{query}'\ | 
					
						
							|  |  |  |     '&tbm=nws'\ | 
					
						
							|  |  |  |     '&gws_rd=cr'\ | 
					
						
							|  |  |  |     '&{search_options}' | 
					
						
							|  |  |  | time_range_attr = "qdr:{range}" | 
					
						
							|  |  |  | time_range_dict = {'day': 'd', | 
					
						
							|  |  |  |                    'week': 'w', | 
					
						
							| 
									
										
										
										
											2016-12-11 16:39:12 +01:00
										 |  |  |                    'month': 'm', | 
					
						
							|  |  |  |                    'year': 'y'} | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  | # do search-request | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  | def request(query, params): | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  |     search_options = { | 
					
						
							|  |  |  |         'start': (params['pageno'] - 1) * number_of_results | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if params['time_range'] in time_range_dict: | 
					
						
							|  |  |  |         search_options['tbs'] = time_range_attr.format(range=time_range_dict[params['time_range']]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if safesearch and params['safesearch']: | 
					
						
							|  |  |  |         search_options['safe'] = 'on' | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  |     params['url'] = search_url.format(query=urlencode({'q': query}), | 
					
						
							|  |  |  |                                       search_options=urlencode(search_options)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if params['language'] != 'all': | 
					
						
							| 
									
										
										
										
											2016-08-06 06:34:56 +02:00
										 |  |  |         language_array = params['language'].lower().split('-') | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  |         params['url'] += '&lr=lang_' + language_array[0] | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  |     return params | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  | # get response from search-request | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  | def response(resp): | 
					
						
							|  |  |  |     results = [] | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  |     dom = html.fromstring(resp.text) | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  |     # parse results | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  |     for result in dom.xpath('//div[@class="g"]|//div[@class="g _cy"]'): | 
					
						
							| 
									
										
										
										
											2017-01-10 11:03:05 +01:00
										 |  |  |         try: | 
					
						
							|  |  |  |             r = { | 
					
						
							|  |  |  |                 'url': result.xpath('.//div[@class="_cnc"]//a/@href')[0], | 
					
						
							|  |  |  |                 'title': ''.join(result.xpath('.//div[@class="_cnc"]//h3//text()')), | 
					
						
							|  |  |  |                 'content': ''.join(result.xpath('.//div[@class="st"]//text()')), | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         except: | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-23 12:59:03 +01:00
										 |  |  |         imgs = result.xpath('.//img/@src') | 
					
						
							|  |  |  |         if len(imgs) and not imgs[0].startswith('data'): | 
					
						
							|  |  |  |             r['img_src'] = imgs[0] | 
					
						
							| 
									
										
										
										
											2016-12-11 01:03:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         results.append(r) | 
					
						
							| 
									
										
										
										
											2014-09-01 15:10:05 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # return results | 
					
						
							| 
									
										
										
										
											2014-03-04 13:11:04 +01:00
										 |  |  |     return results |