1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- from urllib import urlencode
- from cgi import escape
- from lxml import html
- from yaml import load
- import re
- categories = ['images']
- paging = True
- base_url = 'https://www.bing.com/'
- search_string = 'images/search?{query}&count=10&first={offset}'
- def request(query, params):
- offset = (params['pageno'] - 1) * 10 + 1
-
- language = 'en-US'
- search_path = search_string.format(
- query=urlencode({'q': query}),
- offset=offset)
- params['cookies']['SRCHHPGUSR'] = \
- 'NEWWND=0&NRSLT=-1&SRCHLANG=' + language.split('-')[0]
- params['url'] = base_url + search_path
- return params
- def response(resp):
- results = []
- dom = html.fromstring(resp.content)
-
- p = re.compile( '({|,)([a-z]+):(")')
-
- for result in dom.xpath('//div[@class="dg_u"]'):
- link = result.xpath('./a')[0]
-
- yaml_data = load(p.sub( r'\1\2: \3', link.attrib.get('m')))
-
- title = link.attrib.get('t1')
-
- url = yaml_data.get('surl')
- img_src = yaml_data.get('imgurl')
-
- results.append({'template': 'images.html',
- 'url': url,
- 'title': title,
- 'content': '',
- 'img_src': img_src})
-
- if len(results) >= 10:
- break
-
- return results
|