2013-11-09 17:39:20 +00:00
|
|
|
from urllib import urlencode
|
2013-10-19 16:29:39 +00:00
|
|
|
from lxml import html
|
2013-10-24 21:43:39 +00:00
|
|
|
from urlparse import urlparse
|
2013-10-19 16:29:39 +00:00
|
|
|
from cgi import escape
|
|
|
|
|
2014-01-19 20:20:07 +00:00
|
|
|
base_url = None
|
|
|
|
search_url = None
|
2013-10-19 16:29:39 +00:00
|
|
|
|
|
|
|
def request(query, params):
|
|
|
|
global search_url
|
2013-11-09 17:39:20 +00:00
|
|
|
query = urlencode({'q': query})[2:]
|
2013-10-19 16:29:39 +00:00
|
|
|
params['url'] = search_url
|
|
|
|
params['method'] = 'POST'
|
|
|
|
params['data'] = {'query': query}
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
|
|
|
def response(resp):
|
|
|
|
global base_url
|
|
|
|
results = []
|
2013-10-24 19:00:44 +00:00
|
|
|
dom = html.fromstring(resp.content)
|
2014-01-05 13:00:10 +00:00
|
|
|
# ads xpath //div[@id="results"]/div[@id="sponsored"]//div[@class="result"]
|
|
|
|
# not ads : div[@class="result"] are the direct childs of div[@id="results"]
|
|
|
|
for result in dom.xpath('//div[@id="results"]/div[@class="result"]'):
|
2013-10-19 16:29:39 +00:00
|
|
|
link = result.xpath('.//h3/a')[0]
|
2013-10-24 21:43:39 +00:00
|
|
|
url = link.attrib.get('href')
|
2013-10-19 18:12:22 +00:00
|
|
|
parsed_url = urlparse(url)
|
2014-01-05 13:00:10 +00:00
|
|
|
title = link.text_content()
|
2014-01-19 20:20:07 +00:00
|
|
|
|
|
|
|
content =''
|
|
|
|
if len(result.xpath('./p[@class="desc"]')):
|
|
|
|
content = result.xpath('./p[@class="desc"]')[0].text_content()
|
|
|
|
|
2013-10-19 16:29:39 +00:00
|
|
|
results.append({'url': url, 'title': title, 'content': content})
|
2014-01-19 20:20:07 +00:00
|
|
|
|
2013-10-19 16:29:39 +00:00
|
|
|
return results
|