Created
July 3, 2012 08:20
-
-
Save dizpers/3038443 to your computer and use it in GitHub Desktop.
Using Tornado and PyQuery for parsing myshows.ru links for user profiles
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment