1. 首页 > python

python使用pyppeteer 实现bilibili的点选文字验证码登录

import asyncio
from pyppeteer import launch
from pyppeteer.errors import TimeoutError
import random
import base64
import  json
import  requests

def base64_api(uname, pwd, img, typeid):
    with open(img, 'rb') as f:
        base64_data = base64.b64encode(f.read())
        b64 = base64_data.decode()
    data = {"username": uname, "password": pwd, "typeid": typeid, "image": b64}
    result = json.loads(requests.post("http://api.ttshitu.com/predict", json=data).text)
    if result['success']:
        return result["data"]["result"]
    else:
        #!!!!!!!注意:返回 人工不足等 错误情况 请加逻辑处理防止脚本卡死 继续重新 识别
        return result["message"]
    return ""
def getImgCodeText(imgPath,imgType):
    result = base64_api(uname='88888',pwd='88888',img = imgPath,typeid=imgType)
    return result

async def main():
    browser = await launch({
        "headless":False,
        "args":['--window-size=1920,1080'],
        # "defaultViewport": {
        #     "width": 1920,
        #     "height": 1080,
        # },

    })
    page= await  browser.newPage()
    await page.setViewport({"width":1920,"height":1080})
    await page.goto("https://passport.bilibili.com/login")


    await  page.type('input[placeholder="请输入账号"]',"88888",{
        "delay":random.randint(30,60)
    })
    await  page.type('input[placeholder="请输入密码"]',"989565665",{
        "delay":random.randint(30,60)
    })
    await  page.click('div[class="btn_primary "]')

    try:
        await  page.waitForSelector('.geetest_item.geetest_big_item', timeout=30000)
        ele = await  page.J(".geetest_big_item")
        await page.waitFor(3000)
        await  ele.screenshot({'path':'code.png'})
        await page.waitFor(3000)
        result =getImgCodeText('./code.png',27)
        print(result)
        result_list=result.split('|')
        print("result_list:::",result_list)
        code_box = await page.querySelector('.geetest_item.geetest_big_item')
        print("code_box:::", code_box)
        code_box_info=await code_box.boundingBox()
        print("code_box_info:::", code_box_info)
        for pos in result_list:
            x = int(pos.split(',')[0])
            y=int(pos.split(',')[1])
            print("x,y:::",x,y)
            x= code_box_info['x']+x
            y= code_box_info['y']+y
            await page.mouse.move(x,y)
            await page.mouse.down()
            await page.waitFor(3000)
            await  page.mouse.up()
            await page.waitFor(3000)
        await page.click('.geetest_commit')
        await page.waitFor(3000)




    finally:
        print("ok")

    await browser.close()


asyncio.run(main())


哔哩哔哩网站的验证码破解实现原理。记着我使用的是图鉴,你记着把里面的用户名和密码更换一下就行了。

还有其他免费 的方式github上有就不介绍了