Skip to content

Instantly share code, notes, and snippets.

@ryanmcgrath
Created April 9, 2012 04:53
Show Gist options
  • Save ryanmcgrath/2341517 to your computer and use it in GitHub Desktop.
Save ryanmcgrath/2341517 to your computer and use it in GitHub Desktop.
Because Quora doesn't have a damn API.
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib2 # Quora no like requests requests. ;_;
from pyquery import PyQuery as pq
from pprint import pprint
class Puora(object):
def __init__(self, username):
self.answers_url = 'http://www.quora.com/%s/answers' % username
def get_answers(self):
request = urllib2.urlopen(self.answers_url).read()
document = pq(request)
questions_node = document.find('.main').eq(1).find('.main_col').eq(1).find('.question_link')
answers_node = document.find('.main').eq(1).find('.main_col').eq(1).find('.feed_item_answer_content').remove('.feed_item_answer_user').remove('.answer_voters')
questions = [question for question in questions_node.map(lambda i, node: pq(node).text())]
answers = [answer for answer in answers_node.map(lambda i, node: pq(node).text())]
return {'questions': questions, 'answers': answers}
p = Puora('Ryan-McGrath')
print p.get_answers()
@ryanmcgrath
Copy link
Author

I don't have the time, energy nor wherewithal to actually build and maintain such a library, but if I did, it'd probably start out like this. Scraping Quora is kind of annoying - cache this data and such, be smart etc etc.

Now, if Quora would just have an API, we wouldn't have to do things like this...

@sneakyness
Copy link

Ur so hxc

@kennethreitz
Copy link

What does it do to requests requests?

@ryanmcgrath
Copy link
Author

It returns nothing, for some reason. 200 HTTP response, but no body or anything. Would presume possibly headers related or some such crap.

@kennethreitz
Copy link

that's... odd..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment