mirror of
https://github.com/searxng/searxng
synced 2024-01-01 18:24:07 +00:00
[fix] pep8
This commit is contained in:
parent
d959cb1c05
commit
611f4e2a86
@ -1,15 +1,15 @@
|
|||||||
## Google (Web)
|
# Google (Web)
|
||||||
#
|
#
|
||||||
# @website https://www.google.com
|
# @website https://www.google.com
|
||||||
# @provide-api yes (https://developers.google.com/custom-search/)
|
# @provide-api yes (https://developers.google.com/custom-search/)
|
||||||
#
|
#
|
||||||
# @using-api no
|
# @using-api no
|
||||||
# @results HTML
|
# @results HTML
|
||||||
# @stable no (HTML can change)
|
# @stable no (HTML can change)
|
||||||
# @parse url, title, content, suggestion
|
# @parse url, title, content, suggestion
|
||||||
|
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
from urlparse import unquote,urlparse,parse_qsl
|
from urlparse import urlparse, parse_qsl
|
||||||
from lxml import html
|
from lxml import html
|
||||||
from searx.engines.xpath import extract_text, extract_url
|
from searx.engines.xpath import extract_text, extract_url
|
||||||
|
|
||||||
@ -23,10 +23,13 @@ google_hostname = 'www.google.com'
|
|||||||
search_path = '/search'
|
search_path = '/search'
|
||||||
redirect_path = '/url'
|
redirect_path = '/url'
|
||||||
images_path = '/images'
|
images_path = '/images'
|
||||||
search_url = 'https://' + google_hostname + search_path + '?{query}&start={offset}&gbv=1'
|
search_url = ('https://' +
|
||||||
|
google_hostname +
|
||||||
|
search_path +
|
||||||
|
'?{query}&start={offset}&gbv=1')
|
||||||
|
|
||||||
# specific xpath variables
|
# specific xpath variables
|
||||||
results_xpath= '//li[@class="g"]'
|
results_xpath = '//li[@class="g"]'
|
||||||
url_xpath = './/h3/a/@href'
|
url_xpath = './/h3/a/@href'
|
||||||
title_xpath = './/h3'
|
title_xpath = './/h3'
|
||||||
content_xpath = './/span[@class="st"]'
|
content_xpath = './/span[@class="st"]'
|
||||||
@ -36,15 +39,18 @@ images_xpath = './/div/a'
|
|||||||
image_url_xpath = './@href'
|
image_url_xpath = './@href'
|
||||||
image_img_src_xpath = './img/@src'
|
image_img_src_xpath = './img/@src'
|
||||||
|
|
||||||
|
|
||||||
# remove google-specific tracking-url
|
# remove google-specific tracking-url
|
||||||
def parse_url(url_string):
|
def parse_url(url_string):
|
||||||
parsed_url = urlparse(url_string)
|
parsed_url = urlparse(url_string)
|
||||||
if parsed_url.netloc in [google_hostname, ''] and parsed_url.path==redirect_path:
|
if (parsed_url.netloc in [google_hostname, '']
|
||||||
|
and parsed_url.path == redirect_path):
|
||||||
query = dict(parse_qsl(parsed_url.query))
|
query = dict(parse_qsl(parsed_url.query))
|
||||||
return query['q']
|
return query['q']
|
||||||
else:
|
else:
|
||||||
return url_string
|
return url_string
|
||||||
|
|
||||||
|
|
||||||
# do search-request
|
# do search-request
|
||||||
def request(query, params):
|
def request(query, params):
|
||||||
offset = (params['pageno'] - 1) * 10
|
offset = (params['pageno'] - 1) * 10
|
||||||
@ -52,7 +58,7 @@ def request(query, params):
|
|||||||
if params['language'] == 'all':
|
if params['language'] == 'all':
|
||||||
language = 'en'
|
language = 'en'
|
||||||
else:
|
else:
|
||||||
language = params['language'].replace('_','-').lower()
|
language = params['language'].replace('_', '-').lower()
|
||||||
|
|
||||||
params['url'] = search_url.format(offset=offset,
|
params['url'] = search_url.format(offset=offset,
|
||||||
query=urlencode({'q': query}))
|
query=urlencode({'q': query}))
|
||||||
@ -74,19 +80,21 @@ def response(resp):
|
|||||||
try:
|
try:
|
||||||
url = parse_url(extract_url(result.xpath(url_xpath), search_url))
|
url = parse_url(extract_url(result.xpath(url_xpath), search_url))
|
||||||
parsed_url = urlparse(url)
|
parsed_url = urlparse(url)
|
||||||
if parsed_url.netloc==google_hostname and parsed_url.path==search_path:
|
if (parsed_url.netloc == google_hostname
|
||||||
|
and parsed_url.path == search_path):
|
||||||
# remove the link to google news
|
# remove the link to google news
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if parsed_url.netloc==google_hostname and parsed_url.path==images_path:
|
if (parsed_url.netloc == google_hostname
|
||||||
|
and parsed_url.path == images_path):
|
||||||
# images result
|
# images result
|
||||||
results = results + parse_images(result)
|
results = results + parse_images(result)
|
||||||
else:
|
else:
|
||||||
# normal result
|
# normal result
|
||||||
content = extract_text(result.xpath(content_xpath)[0])
|
content = extract_text(result.xpath(content_xpath)[0])
|
||||||
# append result
|
# append result
|
||||||
results.append({'url': url,
|
results.append({'url': url,
|
||||||
'title': title,
|
'title': title,
|
||||||
'content': content})
|
'content': content})
|
||||||
except:
|
except:
|
||||||
continue
|
continue
|
||||||
@ -99,12 +107,13 @@ def response(resp):
|
|||||||
# return results
|
# return results
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
|
||||||
def parse_images(result):
|
def parse_images(result):
|
||||||
results = []
|
results = []
|
||||||
for image in result.xpath(images_xpath):
|
for image in result.xpath(images_xpath):
|
||||||
url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
|
url = parse_url(extract_text(image.xpath(image_url_xpath)[0]))
|
||||||
img_src = extract_text(image.xpath(image_img_src_xpath)[0])
|
img_src = extract_text(image.xpath(image_img_src_xpath)[0])
|
||||||
|
|
||||||
# append result
|
# append result
|
||||||
results.append({'url': url,
|
results.append({'url': url,
|
||||||
'title': '',
|
'title': '',
|
||||||
|
Loading…
Reference in New Issue
Block a user