Last active
June 19, 2018 09:08
-
-
Save freelze/2df85fa30731295a8400cef5971afed2 to your computer and use it in GitHub Desktop.
定時爬宿網流量,若超過一定值將發出Line-Notify的提醒(使用Selenium)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You Need to change 5 values: Chrome location, studentID, password, LINE_TOKEN , DataFlow | |
# Put chromedriver.exe in the same location with this python program. | |
# Download chromedriver.exe: http://chromedriver.chromium.org/downloads | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.common.by import By | |
from selenium.webdriver.common.keys import Keys | |
import schedule # Python job scheduling for humans. | |
import time | |
import requests | |
from bs4 import BeautifulSoup | |
def job(): | |
print("Schedule Job is working...") | |
# driver = webdriver.Chrome(r"C:\Software\chromedriver_win32\chromedriver.exe") # This line will pop-up the chrome window. | |
# Headless Mode: You won't see the pop-up chrome window. | |
chrome_options = Options() | |
chrome_options.add_argument('--headless') | |
chrome_options.add_argument('--disable-gpu') | |
chrome_options.binary_location = r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' # Chrome location | |
driver = webdriver.Chrome(chrome_options = chrome_options) | |
driver.get("https://flowweb.yzu.edu.tw/register/index.php") | |
driver.find_element_by_name("student_id").clear() | |
driver.find_element_by_name("student_id").send_keys("帳號") # s + studentID | |
driver.find_element_by_name("mpwd").clear() | |
driver.find_element_by_name("mpwd").send_keys("密碼") # password | |
driver.find_element_by_name("loginFrame").submit() | |
html_source = driver.page_source # 取得html資料 | |
driver.quit() | |
soup = BeautifulSoup(html_source, 'html.parser') | |
# 方法1 | |
data = soup.findAll('font', {'color': 'red'})[1].string | |
# 方法2 | |
"""從瀏覽器上複製的css選擇器 | |
".sqlDataTable > tbody:nth-of-type(1) > tr:nth-of-type(3) > td:nth-of-type(15) > font:nth-of-type(1)" | |
css = "tbody:nth-of-type(2) > tr:nth-of-type(3) > td:nth-of-type(8)" | |
data = soup.select(css)[0].string""" | |
data = data[0:-1] # XXXXM 把 M去除 | |
MB = int(data) # covert string to integer | |
def lineNotify(token, msg): | |
url = "https://notify-api.line.me/api/notify" | |
headers = { | |
"Authorization": "Bearer " + token, | |
"Content-Type": "application/x-www-form-urlencoded" | |
} | |
payload = {'message': msg} | |
r = requests.post(url, headers=headers, params=payload) | |
return r.status_code | |
token = "LINE_TOKEN" # 你的 LINE NOTIFY TOKEN | |
msg = "宿網流量達到{}M".format(MB) | |
print(msg) | |
DataFlow = 50 | |
if (MB > DataFlow): | |
print(msg) | |
lineNotify(token, msg) | |
schedule.every(1).minutes.do(job) | |
while True: | |
schedule.run_pending() | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment