-
Notifications
You must be signed in to change notification settings - Fork 2
/
spoj.py
35 lines (26 loc) · 1.21 KB
/
spoj.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
import requests
from bs4 import BeautifulSoup, NavigableString
class Spoj():
url = ""
problems_url = ""
def __init__(self):
self.url = "https://www.spoj.com/users/"
self.problems_url = "https://www.spoj.com/problems/"
def is_valid_user(self, username):
url = self.url + username
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
article = soup.find('table', attrs={'class': 'table-condensed'})
return article is not None
def get_user_problems(self, username):
url = self.url + username
page = requests.get(url)
soup = BeautifulSoup(page.content, 'html.parser')
article = soup.find('table', attrs={'class': 'table-condensed'})
data = [row for lines in article for row in lines if not isinstance(row, NavigableString) and row is not None]
problems = [str(code) for problem in data for problem_code in problem for code in problem_code if code not in ["", " ", "\n"]]
urls = [str(self.problems_url + user_problem) for user_problem in problems]
return problems, urls
if __name__ == "__main__":
sp = Spoj()
sp.get_user_problems("ishpreet")