最近在使用xx流量的时候,发现流量不够用,所以写了一个脚本用来自动签到来获取流量
class FeiniaoSign
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 class FeiniaoSign : def __init__ (self ): url = 'https://xxx/auth/login' self .browser = open_browser(url) def input_account (self ): """ 输入账号 """ account_ele = self .browser.find_element(by=By.ID, value='email' ) account_ele.send_keys('renchengqi12@gmail.com' ) logging.info('账号ele:' + self .check_element_value(account_ele)) def input_password (self ): """ 输入密码 """ pwd_ele = self .browser.find_element(by=By.ID, value='passwd' ) pwd_ele.send_keys('NoD7nhA3_Yg' ) logging.info('密码ele:' + self .check_element_value(pwd_ele)) def click_login_btn (self ): """ 点击登录按钮 """ submit_btn = self .browser.find_element(by=By.ID, value='login' ) submit_btn.click() logging.info('submit_btn:' + self .check_element_value(submit_btn)) def click_sign_btn (self ): """ 点击签到按钮 """ try : sign_btn = self .browser.find_element(by=By.ID, value='checkin' ) if sign_btn is not None : ActionChains(self .browser).move_to_element(sign_btn).perform() sign_btn.click() logging.info('sign_btn:' + ("正确" if sign_btn is not None else "错误" )) except NoSuchElementException: logging.info('sign_btn:' + '未找到' ) def save_screenshot (self ): """ 截图 """ path = '/www/wwwroot/peanuts.rencheng.cc/script/Python.png' if is_debug: path = 'Python.png' self .browser.save_screenshot(path) logging.info('截图完成' ) def check_element_value (self, element ): if element is not None : return '正确' else : return '错误' def destroy (self ): self .browser.quit()
.py main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 import osimport sysfrom time import sleepimport loggingfrom selenium import webdriverfrom selenium.common.exceptions import NoSuchElementExceptionfrom selenium.webdriver.common.by import Byfrom apscheduler.schedulers.blocking import BlockingSchedulerdef logger_config (): """ 打印器配置 """ logging.basicConfig(level=logging.INFO, format ='%(asctime)s - %(levelname)s - %(message)s' ) def open_browser (url ): """ 打开浏览器并访问地址 :param url: 网页地址 :return: 浏览器 """ binary_location = '/usr/bin/google-chrome' chrome_driver_binary = '/usr/bin/chromedriver' logging.info('打开站点:' + url) options = webdriver.ChromeOptions() options.add_argument( 'user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/98.0.4758.80 Safari/537.36' ) options.add_argument('window-size=1920,1080' ) if not is_debug: options.headless = True os.environ["webdriver.chrome.driver" ] = chrome_driver_binary options.binary_location = binary_location options.add_argument('--disable-gpu' ) options.add_argument('--no-sandbox' ) options.add_argument('--disable-dev-shm-usage' ) browser = webdriver.Chrome(options=options) browser.get(url) return browser is_debug = False logger_config() if __name__ == '__main__' : is_debug = 'debug' in sys.argv logging.info('脚本开始执行: 环境' + ('DEBUG' if is_debug else 'RELEASE' )) __handler() if is_debug: scheduler = BlockingScheduler() scheduler.add_job(__handler, 'interval' , seconds=86400 , id ='test_job2' , timezone='Asia/Shanghai' ) scheduler.start()