Пример #1
0
            #Ping
            ping = browser.find_element_by_css_selector(
                '#verlauf-detail > tbody > tr:nth-child(3) > td:nth-child(3)'
            ).text
            #timestamp
            timestamp = browser.find_element_by_css_selector(
                '#testresult-detail > tbody > tr:nth-child(1) > td:nth-child(2) > span'
            ).text
            print("DL: " + downspeed + " ; UL: " + upspeed + " ; ping: " +
                  ping)
            fields = [timestamp, downspeed, upspeed, ping, browser.current_url]
            with open(os.path.join(os.path.dirname(__file__), 'results.csv'),
                      'a',
                      newline="") as f:
                writer = csv.writer(f)
                writer.writerow(fields)
            # save cookies
            pickle.dump(
                browser.get_cookies(),
                open(os.path.join(os.path.dirname(__file__), 'cookies.pkl'),
                     "wb"))
        except TimeoutException:
            print("Loading took too much time!")
            print(browser.current_url)
    except TimeoutException:
        print("Didn't find accept button")
        print(browser.current_url)

finally:
    browser.close()
    browser.quit()
Пример #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/'

        # 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]
            })
Пример #3
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)