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" )
0 commit comments