Even if I try using Selenium, it doesn’t work and neither dynamic nor static crawling works. Is there something wrong with my code?
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import csv
def get_best_sellers():
options = Options()
service = Service('C:/Users/badug/My_Python/webdriver/chromedriver.exe')
driver = webdriver.Chrome(service=service, options=options)
driver.get('https://www.kyobobook.co.kr/bestSellerNew/bestseller.laf')
wait = WebDriverWait(driver, 20)
best_sellers = []
try:
books = wait.until(EC.visibility_of_all_elements_located((By.XPATH, '//ul[@class="list_type1"]/li')))
for book in books:
title = book.find_element(By.XPATH, './/div[@class="title"]/a').text
author = book.find_element(By.XPATH, './/div[@class="author"]').text
price = book.find_element(By.XPATH, './/div[@class="price"]').text
best_sellers.append([title, author, price])
except Exception as e:
print("오류 발생:", e)
driver.quit()
return best_sellers
def save_to_csv(data, filename):
with open(filename, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['제목', '저자', '가격'])
writer.writerows(data)
if __name__ == '__main__':
best_sellers = get_best_sellers()
save_to_csv(best_sellers, 'best_sellers.csv')
print('CSV 파일이 생성되었습니다.')
Even if I try using Selenium, it doesn’t work and neither dynamic nor static crawling works. Is there something wrong with my code?
New contributor
leet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.