1
+ # Developed By Parth Maniar
2
+ # https://github.com/officialpm
3
+
4
+ # import libs
5
+
1
6
import os
2
7
import sys
3
8
4
9
from bs4 import BeautifulSoup
5
10
from selenium import webdriver
6
11
from webdriver_manager .chrome import ChromeDriverManager
7
12
8
- if not os .path .exists ("problems" ):
13
+ if not os .path .exists ("problems" ): # create problems folder if not exists
9
14
os .makedirs ("problems" )
10
15
11
16
options = webdriver .ChromeOptions ()
12
- options .add_argument ("headless" )
17
+ options .add_argument ("headless" ) # headless chrome option
13
18
14
19
15
20
def scrapeCodeChef (code ):
16
21
try :
17
22
url = f"https://www.codechef.com/problems/{ code } "
18
-
19
- browser = webdriver . Chrome ( ChromeDriverManager ().install (), options = options )
20
-
23
+ browser = webdriver . Chrome (
24
+ ChromeDriverManager ().install (), options = options
25
+ ) # install and open chrome driver
21
26
print ("[OPENING] - " , code )
22
- browser .get (url )
23
-
27
+ browser .get (url ) # open CodeChef Problem URL
24
28
print ("[SCRAPING] - " , code )
25
- soup = BeautifulSoup (browser .page_source , features = "html.parser" )
26
-
27
- head = soup .find_all (["h1" ])
28
- check = soup .find_all (["p" , "h3" , "ul" , "pre" ])
29
-
30
- f = open ("problems/" + url [34 :] + ".txt" , "a" )
31
- f .write (head [1 ].text [:- 7 ] + "\n " )
32
- for i in range (8 , len (check ) - 18 ):
33
- f .write (check [i ].text + "\n " )
34
- f .close ()
35
-
36
- print (f"[FINISHED] - File saved - problems/{ code } .txt" )
29
+ soup = BeautifulSoup (
30
+ browser .page_source , features = "html.parser"
31
+ ) # parse page source
32
+ head = soup .find_all (["h1" ]) # find all h1 tags
33
+ body = soup .find_all (["p" , "h3" , "ul" , "pre" ]) # find all p, h3, ul, pre tags
34
+ f = open ("problems/" + url [34 :] + ".txt" , "a" ) # open .txt file
35
+ f .write (head [1 ].text [:- 7 ] + "\n " ) # write title
36
+ for i in range (8 , len (body ) - 18 ):
37
+ f .write (body [i ].text + "\n " ) # write body
38
+ f .close () # save file
39
+ print (f"[FINISHED] - File saved - problems/{ code } .txt" )
37
40
except Exception :
38
41
print ("Cannot Find CodeChef Problem! - " + code )
39
42
exit (0 )
@@ -43,7 +46,6 @@ def scrapeCodeChef(code):
43
46
try :
44
47
code = sys .argv [1 ]
45
48
except Exception :
46
- print ('Please Enter A CodeChef Problem Code as a' ,
47
- 'Command-Line Argument!' )
49
+ print ("Please Enter A CodeChef Problem Code as a" , "Command-Line Argument!" )
48
50
exit (0 )
49
51
scrapeCodeChef (code )
0 commit comments