Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit f705150

Browse files
committed
Added CodeForces Scraper
1 parent 35dd322 commit f705150

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# CodeForces Problem Scraper
2+
3+
Takes A CodeForces Problem Code As A Command-Line Argument, Scrapes The Appropriate Problem, And Saves It To A Text File.
4+
5+
## Pre-Requisites
6+
7+
Run The Command
8+
9+
`pip install -r requirements.txt`
10+
11+
## Instructions To Run
12+
13+
Run The Command
14+
15+
`python codeforces_scraper.py`
16+
17+
## *Author Name*
18+
[Roopesh V S](https://github.com/roopeshvs)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from bs4 import BeautifulSoup
2+
import requests
3+
import sys
4+
5+
def get_problem_statement(problem_code):
6+
"""
7+
This function takes a Codeforces problem code as input and
8+
scrapes the problem statement from the site and returns
9+
the parsed problem statement.
10+
11+
Args:
12+
problem_code (string): CodeForces Problem Code
13+
Returns:
14+
problem_statement (string): CodeForces Problem
15+
"""
16+
problem_number = problem_code[:-1]
17+
problem_letter = problem_code[-1]
18+
URL = f"https://codeforces.com/problemset/problem/{problem_number}/{problem_letter}"
19+
try:
20+
page = requests.get(URL)
21+
if not page:
22+
raise Exception(page.status_code)
23+
except Exception as e:
24+
print("Cannot Find CodeForces Problem!" + str(e))
25+
exit(0)
26+
soup = BeautifulSoup(page.content, 'html.parser')
27+
soup.find('div', class_ = 'header').decompose()
28+
problem_statement_div = soup.find('div', class_ = "problem-statement")
29+
response = problem_statement_div.text.replace("$$$", "")
30+
row = response.split(' ')
31+
return response
32+
33+
def to_txt(problem_code, problem):
34+
"""
35+
Takes A Problem Code & Its Appropriate Parsed CodeForces Problem And
36+
Prints It To A Text File.
37+
"""
38+
with open(problem_code + '.txt', 'w') as output_file:R
39+
output_file.writelines(problem)
40+
41+
if __name__ == "__main__":
42+
try:
43+
problem_code = sys.argv[1]
44+
except:
45+
print('Please Enter A CodeForces Problem Code as a Command-Line Argument!')
46+
exit(0)
47+
problem = get_problem_statement(problem_code)
48+
to_txt(problem_code, problem)
49+
print(f"Problem {problem_code} Successfully Scraped And Saved To {problem_code}.txt")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bs4
2+
requests

0 commit comments

Comments
 (0)