-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapper.py
44 lines (31 loc) · 1.6 KB
/
scrapper.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
import requests, json, os
from bs4 import BeautifulSoup
files = os.listdir('./')
solved = set([f.split('.')[0] for f in files if 'py' in f and f!='scrapper.py'])
with open('top_interview_150.md', 'w', encoding='utf-8') as f: f.write('')
url = 'https://leetcode.com/studyplan/top-interview-150/'
response = requests.get(url)
# 응답이 HTML인 경우 BeautifulSoup으로 파싱
soup = BeautifulSoup(response.text, 'html.parser')
script_tag = soup.find('script', {'id': '__NEXT_DATA__', 'type': 'application/json'})
if script_tag:
data = json.loads(script_tag.string)
try:
category = data['props']['pageProps']['dehydratedState']['queries'][0]['state']['data']['studyPlanV2Detail']['planSubGroups']
for c in category:
with open('top_interview_150.md', 'a', encoding='utf-8') as f:
f.write(f"""\n\n< {c['name']} >\n""")
cnt = 0
for q in c['questions']:
if q['difficulty'] == 'EASY': diff = '🟡'
elif q['difficulty'] == 'MEDIUM': diff = '🟠'
elif q['difficulty'] == 'HARD': diff = '🔴'
if q['id'] in solved:
f.write(f" - [x] {q['id']}. {q['title']} {diff} [🔗](./{q['id']}.{q['titleSlug']}.py)\n")
cnt+=1
else:
f.write(f" - [ ] {q['id']}. {q['title']} {diff}\n")
f.write(f"{cnt} / {c['questionNum']} ({int(cnt/int(c['questionNum'])*100)}%)")
f.close()
except KeyError as e:
print(f"KeyError: {e}")