내 체육관 슬롯 예약 자동화

12510 단어 pythonproductivity
🎧 이 기사를 들어보세요



저는 UT Arlington의 대학원생입니다. 저는 항상 우리가 인생에서 성공하기 위해서는 신체 건강과 정신 건강을 모두 돌봐야 한다고 생각합니다. 그래서 저는 Fitness Recreation Center(체육관)에서 일상을 시작하고 매일 슬롯을 등록하는 것이 번거롭습니다. covid로 인해 체육관 슬롯이 매우 제한되어 있고 놓치고 싶지 않습니다.

그래서 매일 체육관 슬롯을 자동으로 등록하는 파이썬 스크립트를 작성했습니다.



Heroku.com에서 Python 스크립트 생성 및 배포



전제 조건:
  • 파이썬
  • 셀레늄
  • 웹드라이버 (크롬 웹드라이버 사용했습니다)

  • 의 시작하자:

    1 단계 :

    필요한 모든 모듈 가져오기

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from time import sleep
    from os import environ
    

    2 단계:

    Chrome Webdriver의 위치를 ​​입력하고 브라우저 변수에 할당합니다.

    browser=webdriver.Chrome("/Users/sunilaleti/Documents/chromedriver") 
    

    3단계:

    포털 열기, 인증(이메일 및 비밀번호) 및 슬롯 예약과 같은 몇 가지 등록 단계가 있습니다.

    웹 페이지를 열려면 broswer.get()가 있고 인증 단계를 위해 해당 요소의 XPath를 가져와야 합니다.

    요소의 Xpath를 가져오려면:
    검사 >> 복사 >> 복사 Xpath


    # to click on sign in button
    browser.find_element_by_xpath('//*[@id="divLoginOptions"]/div[2]/div[2]/div/button').click()
    
    # to enter my email for authentication
    browser.find_element_by_xpath('//*[@id="i0116"]').send_keys(environ.get["Email"])
    browser.find_element_by_xpath('//*[@id="idSIButton9"]').click()
    
    # to enter my password
    browser.find_element_by_xpath('//*[@id="i0118"]').send_keys(environ.get["Password"])
    
    # to click on submit
    browser.find_element_by_xpath('/html/body/div/form[1]/div/div/div[2]/div/div/div[1]/div[2]/div[2]/div/div[2]/div/div[3]/div[2]/div/div/div/div/input').click()
    
    # to scroll the window
    browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
    
    # to book gym slot
    browser.find_element_by_xpath('//*[@id="divBookingSlots"]/div/div[1]/div/button').click()
    



    소스 코드:



    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from time import sleep
    from os import environ
    
    
    def Booking():
         browser=webdriver.Chrome("/Users/sunilaleti/Documents/chromedriver") #Enter Location of Chrome driver
        browser.maximize_window()
        try:
            browser.get("https://reclink.uta.edu/booking/5dcc386e-4bd5-4474-80ec-a47472d3963a")
            sleep(2)
            browser.find_element_by_xpath('//*[@id="divLoginOptions"]/div[2]/div[2]/div/button').click()
            browser.find_element_by_xpath('//*[@id="i0116"]').send_keys(environ.get["Email"])
            browser.find_element_by_xpath('//*[@id="idSIButton9"]').click()
            sleep(2)
            browser.find_element_by_xpath('//*[@id="i0118"]').send_keys(environ.get["Password"])
            sleep(2)
            browser.find_element_by_xpath('/html/body/div/form[1]/div/div/div[2]/div/div/div[1]/div[2]/div[2]/div/div[2]/div/div[3]/div[2]/div/div/div/div/input').click()
            browser.execute_script("window.scrollTo(0,document.body.scrollHeight)")
            browser.find_element_by_xpath('//*[@id="divBookingSlots"]/div/div[1]/div/button').click()
            print("Booked Successfully")
    
        except:
            print("Booking Failed")
    
        finally:
            browser.quit()
    
    if __name__ == '__main__':
        Booking()
    

    4단계:

    Python 스크립트를 작성했으므로 이제 이를 Heroku(컨테이너 기반 클라우드 PaaS(Platform as a Service))에 배포해야 합니다. 개발자는 Heroku를 사용하여 최신 앱을 배포, 관리 및 확장합니다.

    이 Python 스크립트를 배포하려면 Procfile 및 requirements.txt 파일도 필요합니다.
    이 파일을 GitHub에 푸시하고 연결 Heroku

    내 GitHub 저장소에서 이 파일을 얻을 수 있습니다.


    알레티수닐 / 자동화체육관슬롯






    그리고 heroku에서 selenium 스크립트를 배포하려면 코드에 추가해야 합니다.

    chrome_options = webdriver.ChromeOptions()
    chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-dev-shm-usage")
    chrome_options.add_argument("--no-sandbox")
    browser = webdriver.Chrome(executable_path=environ.get("CHROMEDRIVER_PATH"), options=chrome_options)
    


    구성 변수에 이 값을 추가합니다.


    5단계:

    Heroku 스케줄러 추가 기능을 추가하여 매일 이 Python 스크립트를 실행하도록 예약



    그게 다야



    좋은 웹페이지 즐겨찾기