【pyppeteer】pyppeteer的基本使用

发布于:2024-05-10 ⋅ 阅读:(27) ⋅ 点赞:(0)

完成功能:
1.使用指定chrome。https://cdn.npmmirror.com/binaries/chromium-browser-snapshots/Win_x64/1294273/chrome-win.zip
2.最大化窗口
3.反检测
4.简单的点击按钮和输入文字
5.进行frame切换
未完成功能:
进行请求过滤

import asyncio
from pyppeteer import launch

def screen_size():
    """使用tkinter获取屏幕大小"""
    import tkinter
    tk = tkinter.Tk()
    width = tk.winfo_screenwidth()
    height = tk.winfo_screenheight()
    tk.quit()
    return width, height
async def intercept_request(req):
    """请求过滤"""
    if req.resourceType in ['image', 'media', 'eventsource', 'websocket']:
        await req.abort()
    else:
        await req.continue_()
async def intercept_response(res):
    resourceType = res.request.resourceType
    if resourceType in ['xhr', 'fetch']:
        resp = await res.text()
        print(resp)
async def main():
    width, height = screen_size()
    browser = await launch(executablePath=r"D:\devtool\chrome-win\chrome.exe"
                           , headless=False
                           , userDataDir="./myUserDataDir"
                           , devtools=False
                           , ignoreDefaultArgs=['--enable-automation']
                           ,args=['--no-sandbox',f'--window-size={width},{height}'])
    page = await browser.newPage()
    # await page.setUserAgent('Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Mobile Safari/537.36')

    await page.setViewport({  # 最大化窗口
        "width": width,
        "height": height
    })

    await page.goto('https://h5.ele.me/login/')
    # await page.goto('https://www.taobao.com/')
    # 修改别人对客户端自动化的检测
    js1 ='() =>{ Object.defineProperties(navigator,{ webdriver:{ get: () => false } }) }'
    js2 = '() => {alert (window.navigator.webdriver)}'
    await page.evaluate(js1)
    # await asyncio.sleep(5)
    # await page.evaluate(js2)

    # # 设置拦截器
    # await page.setRequestInterception(True)
    # page.on('request', intercept_request)
    # page.on('response', intercept_response)
    pframes = page.frames
    for i in pframes:
        for j in i.childFrames:
            con = await j.xpath('//*[@id="login-form"]')
            if con:
                input_sjh = await j.xpath('//*[@id="fm-sms-login-id"]')
                click_yzm = await j.xpath('//*[@id="login-form"]/div[3]/div[2]/a')
                input_yzm = await j.xpath('//*[@id="fm-smscode"]')
                but = await j.xpath('//*[@id="login-form"]/div[6]/button')
                print(input_sjh)
                await input_sjh[0].type('1996831****')
                await click_yzm[0].click()
                ya = input('请输入验证码:')
                await input_yzm[0].type(str(ya))
                await but[0].click()
                await asyncio.sleep(3)
    await page.goto('https://www.ele.me/home/')
    await asyncio.sleep(100)
    await browser.close()
    await asyncio.sleep(100)



asyncio.get_event_loop().run_until_complete(main())

网站公告

今日签到

点亮在社区的每一天
去签到