-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscraping.py
More file actions
49 lines (44 loc) · 1.54 KB
/
Copy pathscraping.py
File metadata and controls
49 lines (44 loc) · 1.54 KB
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
from bs4 import BeautifulSoup
from urllib.request import urlopen
path = '(1) Problems - LeetCode.html'
htmlfile = open(path, 'r', encoding='utf-8').read()
f = open("README.md", "w", encoding='utf-8')
easy = 0
medium = 0
hard = 0
soup = BeautifulSoup(htmlfile, features='lxml')
div = soup.find('div', {"class":"question-list-table"})
table = div.find('table', {"class": "table table-striped"})
rows = table.find_all('tr')
for r in rows:
data = r.find_all('td')
name = ""
number = ""
for td in data:
a = td.find('a', href=True)
if td.get_text() == "" and a == None and name != "":
f.write('||[Java]')
f.write('(./Algorithms/' + name + '.java)')
name = ""
continue
elif td.get_text() == "" and a == None and name == "":
continue
elif a != None and td.get_text() != "":
f.write('|[' + td.get_text().strip() + ']')
f.write('('+a['href']+')')
name = number + '.%20' + td.get_text().replace(" ", "%20").strip()
elif a != None and td.get_text() == "":
f.write('|[Solution]')
f.write('('+a['href']+')')
f.write('|[Java]')
f.write('(./Algorithms/' + name + '.java)')
else:
f.write('|' + td.get_text())
if td.get_text().isdigit(): number = td.get_text()
if td.get_text() == "Easy": easy += 1
if td.get_text() == "Medium": medium += 1
if td.get_text() == "Hard": hard += 1
if td.get_text() == "Easy" or td.get_text() == "Medium" or td.get_text() == "Hard": break
f.write('|\n')
f.write('Easy:' + str(easy) + ' Medium: ' + str(medium) + ' Hard: ' + str(hard))
f.close()