python-爬虫-playwright模块反爬

from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
    #指定浏览器启动的端口
    #打开cmd:chrome --remote-debugging-port=9412 --user-data-dir="F:\\google"
    #--remote-debugging-port=指定的端口
    #--user-data-dir=指定浏览器运行的数据存放的目录、建一个干净的目录
    #当使用这个之前先打开指定端口的浏览器页面再执行程序
    browser = playwright.chromium.connect_over_cdp('http://localhost:10055')   #指定本地浏览器启动
    context = browser.contexts[0]
    page = context.new_page()
    page.goto("")
    page.get_by_placeholder("账号名/邮箱/手机号").click()
    page.get_by_placeholder("账号名/邮箱/手机号").fill("uname")
    page.get_by_placeholder("请输入登录密码").click()
    page.get_by_placeholder("请输入登录密码").fill("passwd")
    page.get_by_role("button", name="登录").click()

    #关闭页面
    page.close()

    context.close() #关闭上下文
    browser.close() #关闭浏览器

with sync_playwright() as playwright:
    run(playwright)

 

posted @ 2024-06-12 16:59  little小新  阅读(160)  评论(0编辑  收藏  举报