deviantart.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  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. results = []
  15. if resp.status_code == 302:
  16. return results
  17. dom = html.fromstring(resp.text)
  18. for result in dom.xpath('//div[contains(@class, "tt-a tt-fh")]'):
  19. link = result.xpath('.//a[contains(@class, "thumb")]')[0]
  20. url = urljoin(base_url, link.attrib.get('href'))
  21. title_links = result.xpath('.//span[@class="details"]//a[contains(@class, "t")]') # noqa
  22. title = ''.join(title_links[0].xpath('.//text()'))
  23. img_src = link.xpath('.//img')[0].attrib['src']
  24. results.append({'url': url,
  25. 'title': title,
  26. 'img_src': img_src,
  27. 'template': 'images.html'})
  28. return results