12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- from urlparse import urljoin
- from cgi import escape
- from urllib import urlencode
- from lxml import html
- categories = ['it']
- paging = True
- url = 'http://stackoverflow.com/'
- search_url = url+'search?{query}&page={pageno}'
- results_xpath = '//div[contains(@class,"question-summary")]'
- link_xpath = './/div[@class="result-link"]//a|.//div[@class="summary"]//h3//a'
- title_xpath = './/text()'
- content_xpath = './/div[@class="excerpt"]//text()'
- def request(query, params):
- params['url'] = search_url.format(query=urlencode({'q': query}),
- pageno=params['pageno'])
- return params
- def response(resp):
- results = []
- dom = html.fromstring(resp.text)
-
- for result in dom.xpath(results_xpath):
- link = result.xpath(link_xpath)[0]
- href = urljoin(url, link.attrib.get('href'))
- title = escape(' '.join(link.xpath(title_xpath)))
- content = escape(' '.join(result.xpath(content_xpath)))
-
- results.append({'url': href,
- 'title': title,
- 'content': content})
-
- return results
|