Skip to content

Instantly share code, notes, and snippets.

@raghavrv
Last active August 25, 2016 13:50
Show Gist options
  • Save raghavrv/afa20d998054d74e3394 to your computer and use it in GitHub Desktop.
Save raghavrv/afa20d998054d74e3394 to your computer and use it in GitHub Desktop.
Get n_solved / n_submitted by scraping the competitive coding websites
import requests
import bs4
def get_codechef_userstats(user_id):
soup = bs4.BeautifulSoup(
requests.get('https://www.codechef.com/users/%s' % user_id).content)
# Parse the data in the first td tag of the 2nd tr tag of the table with id "problem_stats"
stats_row = soup.find('table', {'id': 'problem_stats'}).findAll('tr')[1].findAll('td')
n_solved = int(stats_row[0].text)
n_submitted = int(stats_row[2].text)
return (n_solved, n_submitted)
def get_spoj_userstats(user_id):
soup = bs4.BeautifulSoup(
requests.get('https://www.spoj.com/users/%s' % user_id).content)
stats_row = soup.find('dl').findAll('dd')
n_solved = int(stats_row[0].text)
n_submitted = int(stats_row[1].text)
return n_solved, n_submitted
def get_hackerearth_userstats(user_id):
soup = bs4.BeautifulSoup(requests.get(
'https://www.hackerearth.com/users/pagelets/%s/hackerearth-profile-overview/'
% user_id).content)
stats_row = soup.find('tbody').findAll('td')
n_solved = int(stats_row[0].text)
n_submitted = int(stats_row[2].text)
return n_solved, n_submitted
def get_userstats(site, user_id):
"""Get the (n_solved, n_submitted) for the given user_id"""
user_id = user_id.lower().strip()
site = site.lower().strip()
try:
return globals()["get_%s_userstats" % site](user_id)
except:
return (-1, -1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment