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

Commit 4434dd4

Browse files
committed
💡 add comments and removed empty lines
1 parent ad57deb commit 4434dd4

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
1+
# Developed By Parth Maniar
2+
# https://github.com/officialpm
3+
4+
# import libs
5+
16
import os
27
import sys
38

49
from bs4 import BeautifulSoup
510
from selenium import webdriver
611
from webdriver_manager.chrome import ChromeDriverManager
712

8-
if not os.path.exists("problems"):
13+
if not os.path.exists("problems"): # create problems folder if not exists
914
os.makedirs("problems")
1015

1116
options = webdriver.ChromeOptions()
12-
options.add_argument("headless")
17+
options.add_argument("headless") # headless chrome option
1318

1419

1520
def scrapeCodeChef(code):
1621
try:
1722
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
2126
print("[OPENING] - ", code)
22-
browser.get(url)
23-
27+
browser.get(url) # open CodeChef Problem URL
2428
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")
3740
except Exception:
3841
print("Cannot Find CodeChef Problem! - " + code)
3942
exit(0)
@@ -43,7 +46,6 @@ def scrapeCodeChef(code):
4346
try:
4447
code = sys.argv[1]
4548
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!")
4850
exit(0)
4951
scrapeCodeChef(code)

0 commit comments

Comments
 (0)