Для разбора DOM был использован модуль PyQuery.
https://gist.github.com/3038443
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from tornado import ioloop | |
from tornado import web | |
from tornado import httpclient | |
from tornado.escape import (json, json_decode, json_encode) | |
from pyquery import PyQuery | |
class ParseUsersHandler(web.RequestHandler): | |
def get(self): | |
client = httpclient.HTTPClient() | |
try: | |
response = client.fetch("http://myshows.ru/search/users/") | |
jQuery = PyQuery(response.body) | |
links = [] | |
jQuery("ul.users.users-quad > li > a").each(lambda i, el: links.append(PyQuery(el).attr("href"))) | |
self.write(json_encode(links)) | |
except httpclient.HTTPError, e: | |
print "Error:", e | |
application = web.Application( | |
[ | |
(r"/rest/parser/users", ParseUsersHandler) | |
], | |
cookie_secret="QQ#j(leMvennf^&$(J000KIKh23373b#FFF$e", | |
debug=True | |
) | |
if __name__ == '__main__': | |
import logging | |
logging.getLogger().setLevel(logging.DEBUG) | |
application.listen(6848) | |
ioloop.IOLoop.instance().start() |
Комментариев нет:
Отправить комментарий