pyppeteer實現自動輸入賬號、密碼、驗證碼


網站自動登錄
使用pyppeteer實現自動輸入賬號、密碼
結合驗證碼識別,可實現自動登錄,獲取完整html數據

注:
驗證碼可調用另一篇隨筆中的代碼實現自動識別功能

import asyncio
import requests
from pyppeteer import launch

width, height = 1366, 768

async def main():
    # headless = False 為False:有頭,True為無頭
    # userDataDir 設置當前網站的保存路徑,下次登陸時可不用登陸
    browser = await launch(headless=True, args=['--disable-infobars'], userDataDir='./b2b.139erp.com')
    # 申明一個page對象
    page = await browser.newPage()
    # 瀏覽器設置寬高
    await page.setViewport({'width': width, 'height': height})
    # 要訪問的網址
    await page.goto('http://26617.b2b.139erp.com')
    # 用戶名和密碼
    await page.type('#dhhm','此處為賬號')
    await page.type('#password', '此處為密碼')
    # 等待5秒獲取驗證碼
    await asyncio.sleep(5)
    # 定位驗證碼,保存到當前路徑
    yanzhengma = await page.waitForSelector('#imgVerify')
    await yanzhengma.screenshot({'path': 'yanzhengma.png'})
    while True:
        v_code = input('請輸入驗證碼:')
        if v_code:
            break
    await page.type('#edtSign', v_code)
    # 點擊登陸
    await page.click('#bt_login')

    await asyncio.sleep(3)
    # 關閉頁面 如果return就不用關閉
    # await page.close()
    # 返回cookie
    return await page.cookies()


data_list = asyncio.get_event_loop().run_until_complete(main())
s = requests.Session()
s.cookies.set(data_list[0]['name'], data_list[0]['value'])
html = s.get(url='http://26617.b2b.139erp.com/blackweb/quoteList.action', headers={
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:80.0) Gecko/20100101 Firefox/80.0"}).text
# 此處已登陸成功
# 從html中獲取數據
print(html)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM