-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCodeChef_Scraping.py
146 lines (126 loc) · 5.64 KB
/
CodeChef_Scraping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import re
import urllib.request
from bs4 import BeautifulSoup
import os
import time
import requests
from urllib.request import Request, urlopen
import argparse
print("\nRunning...\n")
#Arguments
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--Contest_name", required=True, help="Contest name , Ex : JULY19B ")
ap.add_argument("-u", "--User_name", required=True, help="User name , Ex : vatsal1999 ")
args = vars(ap.parse_args())
#initialize variable
Contest_name = args["Contest_name"]
User_name = args["User_name"]
Contest_URL = "https://www.codechef.com/{}/".format(Contest_name)
Contest_page = requests.get(Contest_URL)
#Beautify HTML code
Contest_page_content = BeautifulSoup(Contest_page.content,'html.parser')
data = [['Name','Code','Successful Submissions','Accuracy']]
Problem_code_URL = []
# Fatching Main content of competition
for row in Contest_page_content.findAll('tr',attrs={'class':'problemrow'}):
#print(row)
row_data =[]
problem_name = row.findAll('b')[0].text
problem_code = row.findAll('td')[1].text
problem_Successful_Submission = row.findAll('div')[2].text
problem_Accuracy = row.findAll('a')[1].text
row_data.append(problem_name)
row_data.append(problem_code)
row_data.append(problem_Successful_Submission)
row_data.append(problem_Accuracy)
Problem_code_URL.append(problem_code)
data.append(row_data)
# Printing Table of All content competition
dash = '-'*90
for i in range(len(data)):
if i == 0:
print(dash)
print('{:<40s}{:<15s}{:<25s}{:<10s}'.format(data[i][0],data[i][1],data[i][2],data[i][3]))
print(dash)
else:
print('{:<40s}{:<15s}{:<25s}{:<10s}'.format(data[i][0],data[i][1],data[i][2],data[i][3]))
# tacking Choice from user
time.sleep(5)
print("\n\n\nScraping Problem Statement...\n\n")
print("Select: ")
print("\t\t1) Problem_Statement + Solution")
print("\t\t2) Problem_Statement")
print("\t\t3) Solution")
choice = int(input("Select (1/2/3) :"))
# Function for Get Problem statment by Contest name , user name, and url
def Problem_S(Contest_name,User_name,Problem_code_URL):
print("\n\nProblem Statement Scraping...")
# for every Problem statment URL
for url_code in Problem_code_URL:
time.sleep(2)
print(url_code + " Working...")
problem_page_url = Contest_URL + 'problems/' + url_code+'/'
page = requests.get(problem_page_url)
page_contest = BeautifulSoup(page.content,'html.parser')
statement = page_contest.findAll('div',attrs={'class':'content'})
a = statement[1].text
b = re.sub('[$]', '', a)
# Can't fatch All Special character so replacing them by replace function
b = b.replace("\le","<").replace("\ldots","...").replace("\in","∈").replace("\oplus","⊕").replace("\{","{")
b =b.replace("\}","}").replace("\sum_{i=1}^{N-1}","i=1∑N−1").replace("**","").replace("`","").replace("\neq","≠")
b =b.replace("*","").replace("\rightarrow","->").replace("\ge",">").replace(" \cdot ","*").replace("\neq","≠")
file_name = filename = "Problem_Statement/{}/".format(url_code)+"{}.txt".format(url_code)
# Macking Folder for Problem statment
if not os.path.exists(os.path.dirname(file_name)):
try:
os.makedirs(os.path.dirname(file_name))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
f= open(file_name,"w+",encoding='utf-8')
f.write(str(b))
f.close()
print("{} Problem_Statement Done...\n".format(url_code))
# Function for Get Solution by Contest name , user name, and url
def Solution_s(Contest_name,User_name,Problem_code_URL):
print("\n\nSolutions Scraping...")
# for every Problem page url
for url_code in Problem_code_URL:
time.sleep(2)
print("\n"+url_code +" working...")
# make url by solution status
Sloution_Status_url = Contest_URL + "status/" +url_code+","+User_name
page_content = requests.get(Sloution_Status_url)
page = BeautifulSoup(page_content.content,'html.parser')
ID = page.findAll('td',attrs={'width':'60'})
j=1
# for every solution ID getting information
for sub_id in ID:
solution_view_url = "https://www.codechef.com/viewplaintext/" + sub_id.text
req = Request(solution_view_url, headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).read()
webpage = BeautifulSoup(webpage,'html.parser')
z = webpage.find('pre').text
# Macking New Folder for every New Problem statment
filename = "{}_Solutions/{}/".format(User_name,url_code)+"{}_{}.txt".format(url_code,j)
j=j+1
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise
f= open(filename,"w+",encoding='utf-8')
f.write(z)
f.close()
print("{}) {} Solution Done...".format(j-1,url_code))
if(choice == 1):
Problem_S(Contest_name,User_name,Problem_code_URL)
Solution_s(Contest_name,User_name,Problem_code_URL)
elif(choice == 2):
Problem_S(Contest_name,User_name,Problem_code_URL)
elif(choice == 3):
Solution_s(Contest_name,User_name,Problem_code_URL)
else:
print("\nchoice Proper Option\n\n")