Exemple #1
0
 def __init__(self, name='weibo', browser_type=DEFAULT_BROWSER):
     """
     初始化操作, 微博需要声明一个云打码引用
     :param name: 名称微博
     :param browser: 使用的浏览器
     """
     CookiesGenerator.__init__(self, name, browser_type)
     self.name = name
     self.ydm = Yundama(YUNDAMA_USERNAME, YUNDAMA_PASSWORD, YUNDAMA_APP_ID, YUNDAMA_APP_KEY)
Exemple #2
0
 def __init__(self, name='weibo', browser_type=DEFAULT_BROWSER):
     """
     
     :param name:
     :param browser:
     """
     CookiesGenerator.__init__(self, name, browser_type)
     self.name = name
     self.ydm = Yundama(YUNDAMA_USERNAME, YUNDAMA_PASSWORD, YUNDAMA_APP_ID,
                        YUNDAMA_APP_KEY)
class WeiboCookiesGenerator(CookiesGenerator):
    """
    初始化操作, 微博需要声明一个云打码引用
    :param name: 名称微博
    :param browser: 使用的浏览器
    """
    def __init__(self, name='weibo', browser_type=DEFAULT_BROWSER):
        CookiesGenerator.__init__(self, name, browser_type)
        self.name = name
        self.ydm = Yundama(YUNDAMA_USERNAME, YUNDAMA_PASSWORD, YUNDAMA_APP_ID,
                           YUNDAMA_APP_KEY)

    def _success(self, username):
        wait = WebDriverWait(self.browser, 5)
        success = wait.until(
            EC.visibility_of_element_located((By.CLASS_NAME, 'me_portrait_w')))
        if success:
            print('登录成功')
            self.browser.get('http://weibo.cn/')

            if "我的首页" in self.browser.title:
                print(self.browser.get_cookies())
                cookies = {}
                for cookie in self.browser.get_cookies():
                    cookies[cookie["name"]] = cookie["value"]
                print(cookies)
                print('成功获取到Cookies')
                return (username, json.dumps(cookies))

    def new_cookies(self, username, password):
        """
        生成Cookies
        :param username: 用户名
        :param password: 密码
        :return: 用户名和Cookies
        """
        print('Generatinng cookies of', username)
        self.browser.delete_all_cookies()
        self.browser.get('http://my.sina.com.cn/profile/unlogin')
        wait = WebDriverWait(self.browser, 20)

        try:
            login = wait.until(EC.element_to_be_clickable((By.ID, 'hd_login')))
            login.click()
            user = wait.until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR,
                     '.loginformlist input[name="loginname"]')))
            user.send_keys(username)
            psd = wait.until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR,
                     '.loginformlist input[name="password"]')))
            psd.send_keys(password)
            submit = wait.until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR, '.login_btn')))
            submit.click()
            try:
                result = self._success(username)
                if result:
                    return result
            except TimeoutException:
                print('出现验证码,开始识别验证码')
                yzm = wait.until(
                    EC.visibility_of_element_located(
                        (By.CSS_SELECTOR, '.loginform_yzm .yzm')))
                url = yzm.get_attribute('src')
                cookies = self.browser.get_cookies()
                cookie_dict = {}
                for cookie in cookies:
                    cookie_dict[cookie['name']] = cookie['value']
                response = requests.get(url, cookies=cookie_dict)
                #云打码识别结果
                result = self.ydm.identify(stream=response.content)
                if not result:
                    print('验证码识别失败, 跳过识别')
                    return
                door = wait.until(
                    EC.visibility_of_element_located(
                        (By.CSS_SELECTOR,
                         '.loginform_yzm input[name="door"]')))
                door.send_keys(result)
                submit.click()
                result = self._success(username)
                if result:
                    return result
        except WebDriverException as e:
            print(e.args)
Exemple #4
0
class MWeiboCookiesGenerator(CookiesGenerator):
    def __init__(self, name='weibo', browser_type=DEFAULT_BROWSER):
        """
        
        :param name:
        :param browser:
        """
        CookiesGenerator.__init__(self, name, browser_type)
        self.name = name
        self.ydm = Yundama(YUNDAMA_USERNAME, YUNDAMA_PASSWORD, YUNDAMA_APP_ID,
                           YUNDAMA_APP_KEY)

    def _success(self, username):
        wait = WebDriverWait(self.browser, 5)
        success = wait.until(
            EC.visibility_of_element_located((By.CLASS_NAME, 'me_portrait_w')))

        if success:
            print('success')
            self.browser.get('http://m.weibo.cn/')

            if "weibo" in self.browser.title:
                print(self.browser.get_cookies())
                cookies = {}
                for cookie in self.browser.get_cookies():
                    cookies[cookie["name"]] = cookie["value"]
                print(cookies)
                print('get Cookies')
                return (username, json.dumps(cookies))

    def new_cookies(self, username, password):
        """
        generate Cookies
        :param username:
        :param password:
        :return:
        """
        print('Generating Cookies of', username)
        self.browser.delete_all_cookies()
        self.browser.get('http://my.sina.com.cn/profile/unlogin')
        wait = WebDriverWait(self.browser, 20)

        try:
            login = wait.until(
                EC.visibility_of_element_located((By.ID, 'hd_login')))
            login.click()

            user = wait.until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR,
                     '.loginformlist input[name="loginname"]')))
            user.send_keys(username)
            psd = wait.until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR,
                     '.loginformlist input[name="password"]')))
            psd.send_keys(password)
            submit = wait.until(
                EC.visibility_of_element_located(
                    (By.CSS_SELECTOR, '.login_btn')))
            submit.click()
            try:
                result = self._success(username)
                if result:
                    return result
            except TimeoutException:
                print('verify crc')
                yzm = wait.until(
                    EC.visibility_of_element_located(
                        (By.CSS_SELECTOR, '.loginform_yzm .yzm')))
                url = yzm.get_attribute('src')
                cookies = self.browser.get_cookies()

                cookies_dict = {}
                for cookie in cookies:
                    cookies_dict[cookie.get('name')] = cookie.get('value')
                response = requests.get(url, cookies=cookies_dict)
                result = self.ydm.identify(stream=response.content)
                if not result:
                    print('crc failed, skip')
                    return
                door = wait.until(
                    EC.visibility_of_element_located(
                        (By.CSS_SELECTOR,
                         '.loginform_yzm input[name="door"]')))
                door.send_keys(result)
                submit.click()
                result = self._success(username)
                if result:
                    return result
        except WebDriverException as e:
            pass