pyppepeer淘寶登陸

代碼:node

import time
import random
import asyncio
import pyppeteer


class LoginTaoBao:
    """
    類異步
    """
    pyppeteer.DEBUG = True
    page = None

    async def _injection_js(self):
        """注入js
        """
        await self.page.evaluate('''() =>{

                   Object.defineProperties(navigator,{
                     webdriver:{
                       get: () => false
                     }
                   })
                }''')

    async def _init(self):
        """初始化瀏覽器
        """
        browser = await pyppeteer.launch({'headless': False,
                                          'args': [
                                              '--window-size={1300},{600}'
                                              '--disable-extensions',
                                              '--hide-scrollbars',
                                              '--disable-bundled-ppapi-flash',
                                              '--mute-audio',
                                              '--no-sandbox',
                                              '--disable-setuid-sandbox',
                                              '--disable-gpu',
                                          ],
                                          'dumpio': True,
                                          })
        self.page = await browser.newPage()
        # 設置瀏覽器頭部
        await self.page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
                                     '(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299')
        # 設置瀏覽器大小
        await self.page.setViewport({'width': 1200, 'height': 600})

    async def get_cookie(self):
        cookies_list = await self.page.cookies()
        cookies = ''
        for cookie in cookies_list:
            str_cookie = '{0}={1};'
            str_cookie = str_cookie.format(cookie.get('name'), cookie.get('value'))
            cookies += str_cookie
        print(cookies)
        return cookies

    async def mouse_slider(self):
        """滑動滑塊
        """
        await asyncio.sleep(3)
        try:
            await self.page.hover('#nc_1_n1z')
            # 鼠標按下按鈕
            await self.page.mouse.down()
            # 移動鼠標
            await self.page.mouse.move(2000, 0, {'steps': 30})
            # 鬆開鼠標
            await self.page.mouse.up()
            await asyncio.sleep(2)
        except Exception as e:
            print(e, '      :錯誤')
            return None
        else:
            await asyncio.sleep(3)
            # 獲取元素內容
            slider_again = await self.page.querySelectorEval('#nc_1__scale_text', 'node => node.textContent')
            if slider_again != '驗證經過':
                return None
            else:
                print('驗證經過')
                return True

    async def main(self, username_, pwd_):
        """登錄
        """
        # 初始化瀏覽器
        await self._init()
        # 打開淘寶登錄頁面
        await self.page.goto('https://login.taobao.com')
        # 注入js
        await self._injection_js()
        # 點擊密碼登錄按鈕
        await self.page.click('div.login-switch')
        time.sleep(random.random() * 2)
        # 輸入用戶名
        await self.page.type('#TPL_username_1', username_, {'delay': random.randint(100, 151) - 50})
        # 輸入密碼
        await self.page.type('#TPL_password_1', pwd_, {'delay': random.randint(100, 151)})
        time.sleep(random.random() * 2)
        # 獲取滑塊元素
        slider = await self.page.querySelector('#nc_1__scale_text')
        if slider:
            print('有滑塊')
            # 移動滑塊
            flag = await self.mouse_slider()
            if not flag:
                print('滑動滑塊失敗')
                return None
            time.sleep(random.random() + 1.5)
            # 點擊登錄
            print('點擊登錄')
            await self.page.click('#J_SubmitStatic')
            await asyncio.sleep(100)
        else:
            print('沒滑塊')
            # 按下回車
            await self.page.keyboard.press('Enter')


if __name__ == '__main__':
    username = input('淘寶用戶名')
    pwd = input('密碼')
    login = LoginTaoBao()
    loop = asyncio.get_event_loop()
    task = asyncio.ensure_future(login.main(username, pwd))
    loop.run_until_complete(task)
相關文章
相關標籤/搜索