서비스 기획자의 성장기록

[웹크롤링/데이터 수집] 네이버 웹툰에서 요일별 웹툰 목록 가져오기 본문

웹 크롤링/데이터 수집 (Python)

[웹크롤링/데이터 수집] 네이버 웹툰에서 요일별 웹툰 목록 가져오기

Jenny Noh 2023. 11. 2. 17:34

네이버 웹툰 페이지 (https://comic.naver.com/index)

 

 

 

 

 


 

스크립트   

import requests
from bs4 import BeautifulSoup
import time    # 컨텐츠 생성될때까지 sleep()
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import Chrome, ChromeOptions
from webdriver_manager.chrome import ChromeDriverManager

# 크롬 웹브라우저 자동 실행
driver = Chrome(service=Service(ChromeDriverManager().install()), 
                options=ChromeOptions())
driver.get('https://comic.naver.com/webtoon')

time.sleep(5)   # 동적으로 생성되는 페이지의 내용이 완성될 때까지 대기

soup = BeautifulSoup(driver.page_source, 'lxml')

# 요일별 전체 웹툰 CSS 선택자
temp = soup.select_one('#container > div.component_wrap.type2 > div.WeekdayMainView__daily_all_wrap--UvRFc')
# print(temp)

# 요일별 div 태그 (find_all() 함수로 동일 클래스 선택자를 사용하는 모든 태그 검색)
temp = temp.find_all('div', attrs={'class':'WeekdayMainView__daily_all_item--DnTAH'})
print(len(temp))

week = ['월', '화', '수', '목', '금', '토', '일']
for i, w in enumerate(temp):
    print(f'========== {week[i]}요 웹툰 ==========')
    week_list = w.select('ul > li')    # 상위태그가 ul인 모든 li 태그 선택
    for li_tag in week_list:
        print(li_tag.find('span', attrs={'class':'text'}))

 

 

 

 

 

 


  결과  

and more...

 

and more...
and more...

 

이렇게 일요 웹툰까지 출력됩니다. 

 

 

끗!