url='http://www.cgv.co.kr/theaters/?areacode=01&theaterCode=0013&date=20240103'
fromseleniumimportwebdriver
fromselenium.webdriver.common.byimportBy
fromselenium.webdriver.support.uiimportWebDriverWait
fromselenium.webdriver.supportimportexpected_conditionsasEC
#Selenium WebDriver를 초기화합니다. (ChromeDriver 사용 예제)
driver=webdriver.Chrome()
#대상 웹 페이지로 이동합니다.
driver.get(url)
#iframe의 XPath를 찾아서 해당 iframe으로 전환합니다.
iframe_xpath='//iframe[@id="ifrm_movie_time_table"]' # 실제 웹 페이지의 iframe ID에 맞게 수정
iframe=WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, iframe_xpath))
)
driver.switch_to.frame(iframe)
print(driver.page_source)
하지만 나오지 않았음..
그래서 페이지 로드후 iframe 데이터를 가져오려면 다시 기다려야 하는듯 하여, 다음과 같이 코드 작성
url='http://www.cgv.co.kr/theaters/?areacode=01&theaterCode=0013&date=20240103'
fromseleniumimportwebdriver
fromselenium.webdriver.common.byimportBy
fromselenium.webdriver.support.uiimportWebDriverWait
fromselenium.webdriver.supportimportexpected_conditionsasEC
#Selenium WebDriver를 초기화합니다. (ChromeDriver 사용 예제)
driver=webdriver.Chrome()
#대상 웹 페이지로 이동합니다.
driver.get(url)
#iframe의 XPath를 찾아서 해당 iframe으로 전환합니다.
iframe_xpath='//iframe[@id="ifrm_movie_time_table"]' # 실제 웹 페이지의 iframe ID에 맞게 수정
iframe=WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, iframe_xpath))
)
driver.switch_to.frame(iframe)
#iframe 내용이 로드될 때까지 대기합니다.
iframe_content_xpath='//div[@class="showtimes-wrap"]' # 실제 웹 페이지의 iframe 내용에 맞게 수정
iframe_content=WebDriverWait(driver, 1).until(
EC.presence_of_element_located((By.XPATH, iframe_content_xpath))
).get_attribute("outerHTML")
#iframe에서 벗어나 원래의 상위 레벨로 돌아갑니다.
driver.switch_to.default_content()
#WebDriver를 종료합니다.
driver.quit()
이렇게 하면 iframe의 javascriprt된 후의 내용을 볼 수 있음.
그리고 강의에서는 bs4사용했으므로,
soup = BeautifulSoup(iframe_content, 'html.parser')
다음 코드를 추가하여 bs4 모듈로 강의를 진행하도록 하였음.
이후 텔레그램 챗봇에 관련된 내용은 다음 글에서 작성..