|  | @@ -0,0 +1,26 @@
 | 
	
		
			
			|  | 1 | +from lxml import html
 | 
	
		
			
			|  | 2 | +from urllib import urlencode
 | 
	
		
			
			|  | 3 | +from cgi import escape
 | 
	
		
			
			|  | 4 | +
 | 
	
		
			
			|  | 5 | +base_url = 'http://www.bing.com/'
 | 
	
		
			
			|  | 6 | +search_string = 'search?{query}'
 | 
	
		
			
			|  | 7 | +
 | 
	
		
			
			|  | 8 | +def request(query, params):
 | 
	
		
			
			|  | 9 | +    search_path = search_string.format(query=urlencode({'q': query}))
 | 
	
		
			
			|  | 10 | +    #if params['category'] == 'images':
 | 
	
		
			
			|  | 11 | +    #    params['url'] = base_url + 'images/' + search_path
 | 
	
		
			
			|  | 12 | +    params['url'] = base_url + search_path
 | 
	
		
			
			|  | 13 | +    return params
 | 
	
		
			
			|  | 14 | +
 | 
	
		
			
			|  | 15 | +
 | 
	
		
			
			|  | 16 | +def response(resp):
 | 
	
		
			
			|  | 17 | +    global base_url
 | 
	
		
			
			|  | 18 | +    results = []
 | 
	
		
			
			|  | 19 | +    dom = html.fromstring(resp.content)
 | 
	
		
			
			|  | 20 | +    for result in dom.xpath('//div[@class="sa_cc"]'):
 | 
	
		
			
			|  | 21 | +        link = result.xpath('.//h3/a')[0]
 | 
	
		
			
			|  | 22 | +        url = link.attrib.get('href')
 | 
	
		
			
			|  | 23 | +        title = ' '.join(link.xpath('.//text()'))
 | 
	
		
			
			|  | 24 | +        content = escape(' '.join(result.xpath('.//p//text()')))
 | 
	
		
			
			|  | 25 | +        results.append({'url': url, 'title': title, 'content': content})
 | 
	
		
			
			|  | 26 | +    return results
 |