from selenium import webdriver as wb
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup as bs
import time
import pandas as pd
url = 'https://www.hsd.co.kr/menu/menu_list'
driver = wb.Chrome()
driver.get(url)
# 예외처리(try except문)
# 더보기 버튼요소를 3번 클릭하기
btn_more = driver.find_element_by_class_name('c_05')
try:
for index in range(50):
btn_more.click()
time.sleep(2) #2초동안 멈춤
except:
print(index)
#모든 도시락 정보가 나온 경우 bs4로 변환
soup = bs(driver.page_source,'lxml')
titles = []
prices = []
title_list = soup.findAll('h4', class_='h fz_03')
price_list = soup.select('div.item-price > strong')
for i in range(len(title_list)):
title = title_list[i].text.strip()
price = price_list[i].text.strip()
titles.append(title)
prices.append(price)
RiceStore_info = {'Menu':titles, 'Price':prices}
RiceStore = pd.DataFrame(RiceStore_info)
RiceStore.set_index('Menu', inplace=True)
RiceStore
RiceStore.to_excel('RiceStore.xlsx', encoding='utf-8-sig')