Skip to content

Commit

Permalink
Merge pull request #58 from rohithpr/master
Browse files Browse the repository at this point in the history
Added question details and answer wiki #37
  • Loading branch information
csu committed Jan 5, 2015
2 parents c7dd840 + ad13ab8 commit a9946ff
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Install using pip:

## Usage

### User statistics

```python
from quora import Quora

Expand All @@ -41,14 +43,61 @@ stats = quora.get_user_stats('Christopher-J-Su')
print stats
```

### Questions
```python
from quora import User, Quora

question = Quora.get_question_stats('what-is-python')

question is
{
'want_answers': 3,
'question_text': u'What is python?',
'topics': [u'Science, Engineering, and Technology', u'Technology', u'Electronics', u'Computers'],
'question_details': None, 'answer_count': 1,
'answer_wiki': '<div class="hidden" id="answer_wiki"><div id="ld_mqcfmt_15628"><div id="__w2_po3p1uM_wiki"></div></div></div>',
}

latest_answers = Quora.get_latest_answers('what-is-python')
```

### Answers
```python
from quora import Quora

# The function can be called in any of the following ways.
answer = Quora.get_one_answer('http://qr.ae/6hARL')
answer = Quora.get_one_answer('6hARL')
answer = Quora.get_one_answer(question, author) # question and answer are variables

answer is
{
'want_answers': 8,
'views': 197,
'author': u'Mayur-P-R-Rohith',
'question_link': u'https://www.quora.com/Does-Quora-similar-question-search-when-posing-a-new-question-work-better-than-the-search-box-above',
'comment_count': 1,
'answer': '...',
'upvote_count': 5,
}

latest_answers = Quora.get_latest_answers('what-is-python')

latest_answer is a list of dicts.

random_answers = get_random_answers(5)

random_answers is a list of 5 random answers.
```

## Features
### Currently implemented
* User statistics
* User activity
* Question statistics
* Answer statistics

### Todo
* Answer statistics
* Detailed user information (followers, following, etc.; not just statistics)

## Contribute
Expand Down
15 changes: 10 additions & 5 deletions quora/quora.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_one_answer(question, author = None):
soup = BeautifulSoup(requests.get('http://qr.ae/' + question).text)
else:
soup = BeautifulSoup(requests.get('http://www.quora.com/' + question + '/answer/' + author).text)
return Answer.scrape_one_answer(soup)
return Quora.scrape_one_answer(soup)

@staticmethod
def scrape_one_answer(soup):
Expand Down Expand Up @@ -86,8 +86,8 @@ def scrape_one_answer(soup):
@staticmethod
def get_latest_answers(question):
soup = BeautifulSoup(requests.get('http://www.quora.com/' + question + '/log').text)
authors = Answer.scrape_latest_answers(soup)
return [Answer.get_one_answer(question, author) for author in authors]
authors = Quora.scrape_latest_answers(soup)
return [Quora.get_one_answer(question, author) for author in authors]

@staticmethod
def scrape_latest_answers(soup):
Expand Down Expand Up @@ -119,11 +119,16 @@ def scrape_question_stats(soup):
want_answers = soup.find('span', attrs={'class' : 'count'}).string
answer_count = soup.find('div', attrs={'class' : 'answer_count'}).next.split()[0]
question_text = list(soup.find('div', attrs = {'class' : 'question_text_edit'}).find('h1').children)[-1]
question_details = soup.find('div', attrs = {'class' : 'question_details_text'}).string
answer_wiki = soup.find('div', attrs = {'class' : 'AnswerWikiArea'}).find('div')

question_dict = {'want_answers' : try_cast_int(want_answers),
'answer_count' : try_cast_int(answer_count),
'question_text' : question_text,
'topics' : topics }
'topics' : topics,
'question_details' : question_details,
'answer_wiki' : str(answer_wiki),
}
return question_dict

@staticmethod
Expand Down Expand Up @@ -153,4 +158,4 @@ def get_user_activity(u):
def get_activity(u):
from user import User
user = User()
return user.get_activity(u)
return user.get_activity(u)

0 comments on commit a9946ff

Please sign in to comment.