1. selenium, schedule, requests ์ค์น
- selenium : ์น ๋ธ๋ผ์ฐ์ ๋ฅผ ์คํํ๊ณ ์ ์ดํ๋ค. ๋ธ๋ผ์ฐ์ ์ ์ง์ ์ ๊ทผํ๋ ๋์ ์น ํ์ด์ง๋์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
- reqeusts : ์ธํฐ๋ท์์ ํ์ผ๊ณผ ์น ํ์ด์ง๋ฅผ ๋ค์ด๋ก๋ ๊ฐ๋ฅํ๋ค.
- schedule : ํน์ ์์
์ ์ผ์ ์ ๋ง์ถฐ ์คํํ ์ ์๋๋ก ์ค์ ๊ฐ๋ฅํ๋ค.
- beautifulsoup : ์น ํ์ด์ง๋ฅผ ์์ฑํ๋ ํ์์ธ HTML์ ๊ตฌ๋ฌธ ๋ถ์ํ๋ฉฐ ์ ์ ์น ํ์ด์ง๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
(์ ๊ฐ ํฌ๋กค๋ง ํ ์น ํ์ด์ง๋ ๋์ ์น ํ์ด์ง๋ก ๊ตฌ์ฑ๋์ด ์๊ธฐ ๋๋ฌธ์ selenium์ ์ฌ์ฉํด๋ณด๊ฒ ์ต๋๋ค ๐ค)
pip3 install selenium
pip3 install requests
pip3 install schedule
2. ํน์ URL์ HTML ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ(=ํฌ๋กค๋ง)
- chrom driver ๋ค์ด๋ก๋ ํ path ์ง์ ํ ์ฝ๋์ dirver ์์ฑํ์ฌ ํฌ๋กค๋ง ํ ์น ํ์ด์ง๋ฅผ ํธ์ถํฉ๋๋ค.
(๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋์ธ ์์ ์ด๋ฏ๋ก ์ค์ ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ ๋๋งํ ํ์๊ฐ ์์ผ๋?! optins.headless = True๋ฅผ ์ ์ฉํด์ค๋๋ค.)
#options.headless = True : ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ ๋๋งํ์ง ์๊ณ ๋ฉ๋ชจ๋ฆฌ ์์์๋ง ์์
์ด ์ด๋ฃจ์ด์ง๋๋ก ํ๊ธฐ ์ํ ์ต์
options = Options()
options.headless = True
driver = webdriver.Chrome(executable_path='./chromedriver', options=options)
driver.get('https://www.broadcom.com/support/security-center/definitions/download/detail?gid=sep14')
https://www.broadcom.com/support/security-center/definitions/download/detail?gid=sep14
Security Center Download Detail
www.broadcom.com
https://chromedriver.chromium.org/downloads
ChromeDriver - WebDriver for Chrome - Downloads
Current Releases If you are using Chrome version 92, please download ChromeDriver 92.0.4515.43 If you are using Chrome version 91, please download ChromeDriver 91.0.4472.101 If you are using Chrome version 90, please download ChromeDriver 90.0.4430.24 If y
chromedriver.chromium.org
3. ํน์ ํญ๋ชฉ ๋ถ๋ฌ์ค๊ธฐ
- ์ํ๋ ํญ๋ชฉ์ ์ ํํ ํ ์ค๋ฅธ์ชฝ ํด๋ฆญ - [copy] - [copy selector]๋ฅผ ์ ํ ํ find_element_by_css_selector()์ ๋ฃ์ด์ค๋๋ค.
(์ ๋ ์๋ ์ฌ์ดํธ์ release date๋ฅผ ๋ถ๋ฌ์์ต๋๋ค. RSS๋ ์๊ณ ๋งค์ผ ์ฌ์ดํธ ๋ค์ด๊ฐ๊ธฐ๋ ์ซ์ด์ ์ง์ ๋ง๋ค์๋ค๋,,, ์ฌ๋ฌ๊ฐ์ง ์๋จ์ ํตํด element๋ฅผ ์ฐพ์ ์ ์๋ ํจ์๊ฐ ๋ค์ํ๊ฒ ์ ๊ณต๋๊ณ ์์ต๋๋ค!)
#find_element_by_css_selector() : ์
๋ ฅํ selector์ ๋ง์กฑํ๋ ๋ชจ๋ ์์ ์ค ์ฒซ ๋ฒ์งธ ์์๋ฅผ ๋ฐํ
time.sleep(3)
data = driver.find_element_by_css_selector('#DownloadDetail > div.tab-page > div > div.tab-content > div.tab-pane.d-print-block.active > div > div:nth-child(15) > table > tbody > tr > td:nth-child(3)')
release_date = data.text
4. Slack ๋ฉ์ธ์ง ๋ณด๋ด๋ ํจ์ ๋ง๋ค๊ธฐ
- Slack์์ App ์์ฑ ํ Webhook URL์ ๋ฐ์ ๋ฃ์ด Slack์ผ๋ก ๋ฉ์ธ์ง๋ฅผ ๋ณด๋ผ ์ ์๋ ํจ์๋ฅผ ๋ง๋ญ๋๋ค.
def send_message(text):
url = 'https://hooks.slack.com/services/์๋ต'
payload = { 'text' : text }
requests.post(url, json=payload)
5. Release Date๋ฅผ ํ์ธํ๋ ํจ์ ๋ง๋ค๊ธฐ
- release date์ today๋ฅผ ๋น๊ตํ์ฌ Slack ๋ฉ์ธ์ง๋ฅผ ๋ฐ์กํ๋ ํจ์๋ฅผ ์คํํฉ๋๋ค. ์คํ ํ 0์ผ๋ก ์ ์๋์๋ a ๋ณ์๋ฅผ 1๋ก ๋ณ๊ฒฝํฉ๋๋ค.(์ค๋ ์คํ๋ ์ ์ด ์์ผ๋ฉด ๊ตณ์ด ๋์ด์ ์คํ๋ ํ์๊ฐ ์์ผ๋?! ์ฝ๋๋ฅผ ์ข ๋ฃ์ํค๊ธฐ ์ํฉ์ ๋๋ค!)
def job():
global a
a = 1
if release_date == today:
send_message("File-Based Protection ํจํด์ด ๋ฆด๋ฆฌ์ฆ๋์์ต๋๋ค.")
6. Schedule ๋ง๋ค๊ธฐ
- 1์๊ฐ์ ํ๋ฒ ์คํํ๋ schedule์ ์์ฑํ๊ณ ์คํํฉ๋๋ค. a๋ฅผ ๋น๊ตํ์ฌ ์คํ๋ ์ ์ด ์๋ค๋ฉด break, ์๋ค๋ฉด continue ํฉ๋๋ค.
(ํฌ๋กค๋ง ํ ํญ๋ชฉ์ ํ๋ฃจ์ ๋๋ฒ ๋ฆด๋ฆฌ์ฆ๋์ง ์๊ธฐ ๋๋ฌธ์?! ์๋ ๊ฒ ๋น๊ต ๋ฐ ์ค์ผ์ค๋ง ํ์์ต๋๋ค. ์ค์ผ์ค ํจ์๋ ๋ถ? ์๊ฐ? ํน์ ์์ผ์ ํน์ ์๊ฐ ๋ฑ ๋ค์ํ๊ฒ ์ ๊ณต๋๊ณ ์์ต๋๋ค!)
#schedule.every().hours.do(job) : ๋ช์๊ฐ ๋จ์๋ก job์ ์คํํ ์ง ์ง์ ํฉ๋๋ค.
schedule.every(1).hours.do(job)
#schedule.run_pending() : schedule์ ์คํํฉ๋๋ค.
while True:
schedule.run_pending()
time.sleep(1)
if a == 1:
break
else:
continue
7. ๊ฒฐ๊ณผ
- ๐ ์ฑ๊ณต! ๐
import time, requests, schedule
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
a = 0
today = time.strftime('X%m/X%d/%Y', time.localtime(time.time())).replace('X0', 'X').replace('X', '')
options = Options()
options.headless = True
driver = webdriver.Chrome(executable_path='./chromedriver', options=options)
driver.get('https://www.broadcom.com/support/security-center/definitions/download/detail?gid=sep14')
time.sleep(3)
data = driver.find_element_by_css_selector('#DownloadDetail > div.tab-page > div > div.tab-content > div.tab-pane.d-print-block.active > div > div:nth-child(15) > table > tbody > tr > td:nth-child(3)')
release_date = data.text
def send_message(text):
url = 'https://hooks.slack.com/services/์๋ต'
payload = { 'text' : text }
requests.post(url, json=payload)
def job():
global a
a = 1
if release_date == today:
send_message("File-Based Protection ํจํด์ด ๋ฆด๋ฆฌ์ฆ๋์์ต๋๋ค.")
schedule.every(1).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
if a == 1:
break
else:
continue
'Programming > Python' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฌธ์์ด ๋ค๋ฃจ๊ธฐ (0) | 2021.08.09 |
---|---|
ํ์ผ ๊ฒฝ๋ก ์ด๋(copy, move)ํ๊ณ ์ญ์ (rm)ํ๊ณ ์์ถ(zip)ํ๊ธฐ (0) | 2021.06.22 |
ํ์ผ ์ด๊ณ (open) ์ฝ๊ณ (read) ์ฐ๊ธฐ(wirte) (0) | 2021.06.17 |
OS ๋ชจ๋ (0) | 2021.06.16 |
์น ํฌ๋กค๋ง(BeautifulSoup) (0) | 2021.05.06 |