Exemplo n.º 1
0
class QCourse:
    def __init__(self):
        # 初始化options
        self.prefs = {"download.default_directory": os.getcwd()}
        self.options = EdgeOptions()
        self.options.use_chromium = True
        self.options.add_argument("log-level=3")
        self.options.add_experimental_option('excludeSwitches',
                                             ['enable-logging'])
        self.options.add_experimental_option('prefs', self.prefs)
        self.options.add_argument("--mute-audio")

        self.login_url = 'https://ke.qq.com/'

        # Mac 下配置 options 报错,故扔掉了。如果是 Windows,请使用路径下面的 msedgedriver.exe。(注释掉下面一行,放开下下行)
        self.driver = Edge(executable_path=os.path.join(
            BASE_DIR, 'msedgedriver'),
                           capabilities={})
        # self.driver = Edge(executable_path='msedgedriver.exe', options=self.options)

        # self.driver = Edge(executable_path=os.path.join(BASE_DIR, 'msedgedriver'), capabilities=desired_cap, options=self.options)

    def login(self):
        self.driver.get('https://ke.qq.com/')
        self.driver.find_element_by_id('js_login').click()
        time.sleep(1)

        WebDriverWait(self.driver, 300).until_not(
            EC.presence_of_element_located((By.CLASS_NAME, 'ptlogin-mask')))

        dictCookies = self.driver.get_cookies()
        jsonCookies = json.dumps(dictCookies)
        with open('cookies.json', 'w') as f:
            f.write(jsonCookies)
        print('登陆成功!')

    def close(self):
        self.driver.close()

    def _get_video(self, video_url=None, path=None, index=None):
        if not video_url:
            print('请输入视频url!')
        # 跳转一次没法跳转,可能是设置了preventDefault
        self.driver.get(video_url)
        self.driver.get(video_url)
        try:
            # 等待视频开始播放
            WebDriverWait(self.driver, 60).until(
                EC.presence_of_element_located((By.CLASS_NAME, 'loki-time')))
            WebDriverWait(
                self.driver,
                60).until_not(lambda driver: driver.find_element_by_class_name(
                    'loki-time').get_attribute("innerHTML") == '00:00 / 00:00')

            title = self.driver.title
            if index is not None:
                title = "{:02}_{}".format(index, title)

            networks = self.driver.execute_script(
                'return window.performance.getEntries()')
            ts_url = key_url = ''
            for network in networks:
                if '.ts?start' in network.get('name'):
                    ts_url = network.get('name')
                elif 'get_dk' in network.get('name'):
                    key_url = network.get('name')
            download_single(ts_url, key_url, title, path)
        except TimeoutException:
            # 如果超时,可能是下载的资料,则查看是否有下载按钮,有的话,就下载
            title = self.driver.title
            try:
                down_btn = self.driver.find_element_by_class_name(
                    'download-btn')
                if down_btn.text == '下载资料':
                    url = down_btn.get_attribute('href')
                    download_zip_doc(url, title, path)
            except Exception:
                print('没有找到视频,也没有找到可下载的文件,可能是还未开课')

    def get_video(self, video_url=None, path=None, index=None):
        if isinstance(video_url, list):
            for url in video_url:
                if url:
                    self._get_video(url, path, index)
        else:
            self._get_video(video_url, path, index)

    def load_cookies(self):
        if not os.path.exists('cookies.json'):
            self.login()
        with open('cookies.json', 'r') as f:
            listCookies = json.loads(f.read())
        self.driver.get(self.login_url)
        for cookie in listCookies:
            self.driver.add_cookie({
                'domain': '.ke.qq.com',
                'httpOnly': cookie['httpOnly'],
                'name': cookie['name'],
                'path': '/',
                'secure': cookie['secure'],
                'value': cookie['value']
            })
        for cookie in utils.get_cookies_dic_list():
            self.driver.add_cookie({
                'domain': '.ke.qq.com',
                'httpOnly': False,
                'name': cookie[0],
                'path': '/',
                'secure': False,
                'value': cookie[1]
            })
Exemplo n.º 2
0
class QCourse:
    def __init__(self):
        # 初始化options
        self.prefs = {"download.default_directory": os.getcwd()}
        self.options = EdgeOptions()
        self.options.use_chromium = True
        self.options.add_argument("log-level=3")
        self.options.add_experimental_option('excludeSwitches',
                                             ['enable-logging'])
        self.options.add_experimental_option('prefs', self.prefs)
        self.options.add_argument("--mute-audio")

        self.login_url = 'https://ke.qq.com/'

        self.driver = Edge(executable_path='msedgedriver.exe',
                           options=self.options)

    def login(self):
        self.driver.get('https://ke.qq.com/')
        self.driver.find_element_by_id('js_login').click()
        time.sleep(1)

        WebDriverWait(self.driver, 300).until_not(
            EC.presence_of_element_located((By.CLASS_NAME, 'ptlogin-mask')))

        dictCookies = self.driver.get_cookies()
        jsonCookies = json.dumps(dictCookies)
        with open('cookies.json', 'w') as f:
            f.write(jsonCookies)
        print('登陆成功!')

    def close(self):
        self.driver.close()

    def get_video(self, video_url=None, path=None):
        if not video_url:
            print('请输入视频url!')
        # os.chdir(BASE_DIR)
        if not os.path.exists('cookies.json'):
            self.login()
        with open('cookies.json', 'r') as f:
            listCookies = json.loads(f.read())
        self.driver.get(video_url)
        for cookie in listCookies:
            self.driver.add_cookie({
                'domain': '.ke.qq.com',
                'httpOnly': cookie['httpOnly'],
                'name': cookie['name'],
                'path': '/',
                'secure': cookie['secure'],
                'value': cookie['value']
            })
        self.driver.get(video_url)
        # 等待视频开始播放
        WebDriverWait(self.driver, 300).until(
            EC.presence_of_element_located((By.CLASS_NAME, 'loki-time')))
        WebDriverWait(
            self.driver,
            300).until_not(lambda driver: driver.find_element_by_class_name(
                'loki-time').get_attribute("innerHTML") == '00:00 / 00:00')

        networks = self.driver.execute_script(
            'return window.performance.getEntries()')
        ts_url = key_url = ''
        for network in networks:
            if '.ts?start' in network.get('name'):
                ts_url = network.get('name')
            elif 'get_dk' in network.get('name'):
                key_url = network.get('name')
        title = self.driver.title
        # catalog = self.driver.execute_script('return document.getElementsByClassName("task-item task-info active")'
        #                                      '[0].parentNode.firstElementChild.innerText')
        # os.chdir(os.path.join(os.getcwd(), catalog))
        download_single(ts_url, key_url, title, path)
Exemplo n.º 3
0
driverOptions.add_experimental_option("excludeSwitches", ["enable-logging"])
browser = Edge(options=driverOptions)

try:
    try:
        browser.get('https://www.netztest.at/')
        cookies = pickle.load(
            open(os.path.join(os.path.dirname(__file__), 'cookies.pkl'), "rb"))

        for cookie in cookies:
            if cookie['name'] == 'RMBTuuid':
                cookie['expiry'] = int(
                    (datetime.datetime.utcnow().replace(
                        hour=0, minute=0, second=0, microsecond=0) -
                     datetime.datetime(1970, 1, 1)).total_seconds() + 1209600)
                browser.add_cookie(cookie)
                print("added cookie ", cookie)
    except Exception:
        print("Couldn't load cookies")

    browser.get('https://www.netztest.at/de/Test')

    delay = 30  # seconds
    #wait for button to allow test and click it
    try:
        button = WebDriverWait(browser, delay).until(
            EC.presence_of_element_located((
                By.CSS_SELECTOR,
                'div.ui-dialog-buttonpane.ui-widget-content > div > button:nth-child(2)'
            )))
        button.click()
Exemplo n.º 4
0
def _init_browser(url: str, str_cookie=None):
    global browser
    if browser:
        # Browser is already initilized
        return browser
    # Initilize the browser
    if (sys.platform == "win32"):
        # If on Windows
        try:
            # Try the Chrome webdriver
            options = ChromeOptions()
            # Add options for imporved performance
            options.add_argument("--disable-extensions")
            options.add_argument("--disable-gpu")
            options.add_argument("--no-sandbox")
            # Run in headless mode
            options.add_argument("--headless")
            # Hide log message
            options.add_experimental_option('excludeSwitches',
                                            ['enable-logging'])
            # Get the webdriver
            browser = webdriver.Chrome(
                executable_path="webdrivers/chromedriver.exe", options=options)
            # Test if Chrome binary exist
            browser.get(url)
            log.debug("initialized Chrome Driver")
        except:
            log.debug("Failed to set-up Chrome webdriver")
            # Else, try the MS Edge webdriver
            try:
                edge_options = EdgeOptions()
                edge_options.use_chromium = True
                # Run in headless mode
                edge_options.add_argument('headless')
                edge_options.add_argument('disable-gpu')
                # Get the webdriver
                browser = Edge(executable_path="webdrivers/msedgedriver.exe",
                               options=edge_options)
                log.debug("initialized Edge Driver")
            except:
                log.error("Could not set-up a webdriver")
                log.debug(
                    "Chrome or Chromium Edge must be installed to scan for DOM-based XSS vulnerability"
                )
                return None
    else:
        # *nix
        try:
            options = FirefoxOptions()
            # Run in headless mode
            options.add_argument("--headless")
            # Get the webdriver
            try:
                browser = webdriver.Firefox(options=options)
            except:
                log.debug("geckodriver is not in PATH")
                browser = webdriver.Firefox(
                    executable_path="webdrivers/geckodriver", options=options)
            # Test if Firefox binary exist
            browser.get(url)
            log.debug("initialized Firefox Driver")
        except:
            log.error("Could not set-up a webdriver")
            log.exception(
                "Firefox must be installed to scan for DOM-based XSS vulnerability"
            )
            return None

    if str_cookie:
        cookie = SimpleCookie()
        cookie.load(str_cookie)
        cookies = {}
        for key, morsel in cookie.items():
            cookies[key] = morsel.value
        browser.get(url)
        # browser.delete_all_cookies()
        for key in cookies.keys():
            value = cookies[key]
            browser.add_cookie({"name": key, "value": value})
    return browser