deviantart.py 1.1KB

123456789101112131415161718192021222324252627282930313233343536
  1. from urllib import urlencode
  2. from urlparse import urljoin
  3. from lxml import html
  4. categories = ['images']
  5. base_url = 'https://www.deviantart.com/'
  6. search_url = base_url+'search?offset={offset}&{query}'
  7. paging = True
  8. def request(query, params):
  9. offset = (params['pageno'] - 1) * 24
  10. params['url'] = search_url.format(offset=offset,
  11. query=urlencode({'q': query}))
  12. return params
  13. def response(resp):
  14. global base_url
  15. results = []
  16. if resp.status_code == 302:
  17. return results
  18. dom = html.fromstring(resp.text)
  19. for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
  20. link = result.xpath('.//a[contains(@class, "thumb")]')[0]
  21. url = urljoin(base_url, link.attrib.get('href'))
  22. title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
  23. title = ''.join(title_links[0].xpath('.//text()'))
  24. img_src = link.xpath('.//img')[0].attrib['src']
  25. results.append({'url': url,
  26. 'title': title,
  27. 'img_src': img_src,
  28. 'template': 'images.html'})
  29. return results