-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (51 loc) · 2.1 KB
/
main.py
File metadata and controls
62 lines (51 loc) · 2.1 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
50
51
52
53
54
55
56
57
58
59
60
61
62
import time
import random
from config import MYSQL_CONFIG, GENRES, BASE_URL, GENRE_MAP
from modules.db_handler import connect_database, save_one_row
from modules.crawler import WebtoonCrawler
# 1차 크롤링: 장르별 웹툰
def main():
# 1. DB 연결
conn = connect_database(MYSQL_CONFIG)
if not conn:
return
cursor = conn.cursor()
# 2. 크롤러 시작
crawler = WebtoonCrawler()
try:
crawler.start_driver()
if not crawler.login():
raise Exception("로그인 실패로 프로그램을 종료합니다.")
print("\n🚀 크롤링을 시작합니다...")
total_saved = 0
# 3. 장르별 순회
for genre_code in GENRES:
target_genre_ko = GENRE_MAP.get(genre_code, genre_code)
print(f"\n=== [장르 수집 시작: {genre_code} ({target_genre_ko})] ===")
urls = crawler.get_genre_urls(BASE_URL + genre_code)
print(f"📊 수집 대상: 총 {len(urls)}개 작품")
for i, url in enumerate(urls, 1):
print(f"[{i}/{len(urls)}] 진행 중...", end='\r')
data = crawler.crawl_detail(url)
if data:
# '로판' 장르 수집 시에만 강제로 장르명 고정
if genre_code == '로판':
data['genre'] = '로판'
if save_one_row(conn, cursor, data):
total_saved += 1
# 봇 탐지 회피를 위한 랜덤 대기
crawler.human_pause(1.5, 3.5)
except KeyboardInterrupt:
print("\n🛑 사용자에 의해 중단되었습니다.")
except Exception as e:
print(f"\n❌ 치명적 오류 발생: {e}")
finally:
# 리소스 정리
if 'crawler' in locals():
crawler.close_driver()
if conn and conn.is_connected():
cursor.close()
conn.close()
print("데이터베이스 연결이 안전하게 종료되었습니다.")
if __name__ == "__main__":
main()