- 미국 여행 도중, 셔틀버스 예약을 해야 입장할 수 있는 관광지가 있었다.
- 그러나 방문하기로 한 날짜가 얼마 남지 않아서 남은 자리는 없는 상황이었다.
- 무작정 새로고침을 계속 누르기보다는, 셀레니움을 통해 일정 간격마다 웹페이지에 접속해서 빈자리가 날 때 텔레그램 메시지를 보내도록 코드를 작성하였다.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from webdriver_manager.chrome import ChromeDriverManager
#from datetime import date
import time
import telegram
# Fixed values
TOKEN = 'my_telegram_token'
CHAT_ID = 'my_telegram_ID'
url = "target_website_url"
now = time
# 목표로 한 시간대(15분마다 버스가 있고, 목표로 한 시간대는 8시, 9시, 10시 시간대)
xpath_list = ["//*[@id='ctl00_leftNav_rptActivityDates_ctl00_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl01_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl02_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl03_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl04_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl05_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl06_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl07_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl08_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl09_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl10_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl11_lblAvailable']",
"//*[@id='ctl00_leftNav_rptActivityDates_ctl12_lblAvailable']"]
# Telegram bot
bot = telegram.Bot(token=TOKEN)
#chrome option settings
options = Options()
#Automatic webdriver version setting, option setting
browser = webdriver.Chrome(service=Service(ChromeDriverManager().install()),options=options)
#compare recent status, send telegram message
while True:
try:
#run the scrapping
browser.get(url)
time.sleep(15)
#switch to iframe
iframe = browser.find_element(By.XPATH,"//iframe[@id='iframeAdventureOffice']")
browser.switch_to.frame(iframe)
elem_date = browser.find_element(By.XPATH, date_set_xpath)#set date
#browser.execute_script("arguments[0].click();",elem_date)
elem_date.send_keys(Keys.ENTER)
#time to load page
time.sleep(5)
print("timestamp : ", now.ctime())
for i_xpath in xpath_list:
elem = browser.find_element(By.XPATH,i_xpath)
elem_text = elem.text
if elem_text == "Sold Out":
print('elem_text:',elem_text)
pass
else:
bot.sendMessage(chat_id=CHAT_ID, text = elem_text)
print('elem_text:',elem_text)
print('send message')
break
# To handle exceptions
except Exception as e:
print(e)
- 코드 실행시 아래와 같이 사이트에 자동으로 접속해 날짜를 설정하고, 시간대별 결과값을 return한다.
- 만약 자리가 나면 각 시간대별 text 값이 "Sold Out"이 아니게 되며, 아래와 같이 메시지가 전송된다. 그러면 사이트에 접속해서 예약을 진행하면 된다.
'코딩 > 뭔가 만들어 보기' 카테고리의 다른 글
[Python] 2015~2020년 대한민국 근로소득 불평등도는 어떻게 변했을까?(지니계수, 로렌츠곡선) (0) | 2022.12.11 |
---|---|
[Python] 특정 페이지에 새 글이 올라오면 알림을 보내는 코드 (0) | 2022.09.14 |
근로소득 구간별 세금 부담 비율 (0) | 2021.09.29 |
내 연봉, 근로소득자 중 상위 몇 퍼센트일까?(+ 세금은 얼마나 내고 있을까?) (0) | 2021.09.07 |
연말정산 계산기 만들어보기(1) (1) | 2021.08.29 |
댓글