-
Notifications
You must be signed in to change notification settings - Fork 2
/
codechef.py
36 lines (27 loc) · 1.33 KB
/
codechef.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
import requests
from bs4 import BeautifulSoup, NavigableString
class Codechef():
url = ""
problems_url = ""
def __init__(self):
self.url = "https://www.codechef.com/users/"
self.problems_url = "https://www.codechef.com/problems/"
def is_valid_user(self, username):
url = self.url + username
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
articles = soup.find_all('article')
data = [row for article in articles for row in article if not isinstance(row, NavigableString) and row is not None and row.span is not None]
return len(data) > 0
def get_user_problems(self, username):
url = self.url + username
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
articles = soup.find_all('article')
data = [row for article in articles for row in article if not isinstance(row, NavigableString) and row is not None and row.span is not None]
problems = [str(problem) for row in data for para in row.span for problem in para if problem not in [' ', ',', ', ']]
urls = [str(self.problems_url + user_problem) for user_problem in problems]
return problems, urls
if __name__ == "__main__":
cc = Codechef()
cc.get_user_problems("ishpreet")