-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathquestions.py
66 lines (60 loc) · 2.14 KB
/
questions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from bs4 import BeautifulSoup
import requests
import json
class StackOverflow:
def __init__(self, topic):
self.topic = topic
def getQuestions(self):
"""
Returns the questions, views, votes, answer counts, and descriptions in JSON format\n
Class - `StackOverflow`
Example:
```
que = StackOverflow(topic="github")
scrape = que.getQuestions()
```
Returns:
{
"question": question title
"views": view count of question
"vote_count": vote count of question
"answer_count": no. of answers to the question
"description": description of the question
}
"""
url = "https://stackoverflow.com/questions/tagged/" + self.topic
try:
res = requests.get(url)
soup = BeautifulSoup(res.text, "html.parser")
questions_data = {"questions": []}
questions = soup.select(".s-post-summary")
for que in questions:
title = que.select_one(".s-link").getText()
stats = que.select(".s-post-summary--stats-item-number")
vote = stats[0].getText()
ans = stats[1].getText()
views = stats[2].getText()
desc = (
que.select_one(".s-post-summary--content-excerpt")
.getText()
.strip()
.encode("ascii", "ignore")
.decode()
.replace(" ", "")
)
questions_data["questions"].append(
{
"question": title,
"views": views,
"vote_count": vote,
"answer_count": ans,
"description": desc,
}
)
json_data = json.dumps(questions_data)
return json_data
except:
error_message = {
"message": "No questions related to the topic found"}
ejson = json.dumps(error_message)
return ejson