Playwright事件监控

2022-12-05 14:27:15 浏览数 (1)

一、Page.on添加监控

1、全部监控事件
  • page.on("close")
  • page.on("console")
  • page.on("crash")
  • page.on("dialog")
  • page.on("domcontentloaded")
  • page.on("download")
  • page.on("filechooser")
  • page.on("frameattached")
  • page.on("framedetached")
  • page.on("framenavigated")
  • page.on("load")
  • page.on("pageerror")
  • page.on("popup")
  • page.on("request")
  • page.on("requestfailed")
  • page.on("requestfinished")
  • page.on("response")
  • page.on("websocket")
  • page.on("worker")
2、Page.goto(url)解析过程

(1)通过网络加载解析页面 (2)触发 page.on("domcontentloaded") 事件 (3)执行页面的 js 脚本,加载静态资源 (4)触发 page.on("laod")事件 (5)页面执行动态加载的脚本 (6)当 500ms 都没有新的网络请求的时候,触发 networkidle 事件

3、监听请求和响应时间
代码语言:javascript复制
from playwright.sync_api import sync_playwright

def run_request(playwright):
    chromium = playwright.chromium  # or "firefox" or "webkit".

    # 手机设备设置为iPhone 12 Pro Max
    iphone_12 = playwright.devices['iPhone 12']
    # 启动谷歌有窗口浏览器
    browser = chromium.launch(headless=False)
    # 配置用户定位信息
    context = browser.new_context(
        **iphone_12,
        locale='zh-CN',
        geolocation={'longitude': 116.39014, 'latitude': 39.913904},
        permissions=['geolocation']
    )

    page = context.new_page()

    # 监听请求事件
    page.on("request", lambda request: print("request: ", request.method, request.url))
    # 监听响应事件
    page.on("response", lambda response: print("response: ", response.status, response.url))

    # 打开高德H5网页
    page.goto("https://m.amap.com/")

    browser.close()


with sync_playwright() as playwright:
    run_request(playwright)

高德H5网页为例,运行后,可查看监听结果:

代码语言:javascript复制
request:  GET https://m.amap.com/
response:  200 https://m.amap.com/
request:  GET https://a.amap.com/pc/static/config/config_m/lbs_m_about.js
request:  GET https://g.alicdn.com/aes/??tracker/1.0.34/index.js,tracker-plugin-jserror/1.0.13/index.js,tracker-plugin-api/1.1.11/index.js,tracker-plugin-perf/1.1.6/index.js,tracker-plugin-longtask/1.0.1/index.js,tracker-plugin-pv/2.4.4/index.js,tracker-plugin-event/1.2.5/index.js
request:  GET https://img.yuanmabao.com/zijie/pic/2022/12/05/h3bisroaxct.png
request:  GET https://webapi.amap.com/maps?v=1.4.15&key=ab403a9b356e63c917ff05b037cd3e7d&plugin=AMap.Scale
request:  GET https://g.alicdn.com/alilog/mlog/aplus_wap.js
request:  GET https://g.alicdn.com/??/AWSC/AWSC/awsc.js,/sd/baxia-entry/baxiaCommon.js
response:  200 https://a.amap.com/pc/static/config/config_m/lbs_m_about.js
response:  200 https://g.alicdn.com/aes/??tracker/1.0.34/index.js,tracker-plugin-jserror/1.0.13/index.js,tracker-plugin-api/1.1.11/index.js,tracker-plugin-perf/1.1.6/index.js,tracker-plugin-longtask/1.0.1/index.js,tracker-plugin-pv/2.4.4/index.js,tracker-plugin-event/1.2.5/index.js
response:  200 https://g.alicdn.com/alilog/mlog/aplus_wap.js
response:  200 https://img.yuanmabao.com/zijie/pic/2022/12/05/h3bisroaxct.png
response:  200 https://g.alicdn.com/??/AWSC/AWSC/awsc.js,/sd/baxia-entry/baxiaCommon.js
response:  200 https://webapi.amap.com/maps?v=1.4.15&key=ab403a9b356e63c917ff05b037cd3e7d&plugin=AMap.Scale
request:  GET https://webapi.amap.com/maps/modules?v=1.4.20&key=ab403a9b356e63c917ff05b037cd3e7d&vrs=1656665904502&m=touch,vectorlayer,overlay,wgl,AMap.Scale,sync
response:  200 https://webapi.amap.com/maps/modules?v=1.4.20&key=ab403a9b356e63c917ff05b037cd3e7d&vrs=1656665904502&m=touch,vectorlayer,overlay,wgl,AMap.Scale,sync
request:  GET https://m.amap.com/_AMapService/v3/log/init?s=rsv3&product=JsInit&key=ab403a9b356e63c917ff05b037cd3e7d&t=1664351120061&resolution=428*746&mob=1&vt=1&dpr=3&scale=1&detect=true&callback=jsonp_435631_&platform=JS&logversion=2.0&appname=https://m.amap.com/&csid=F64A5D3D-16B5-48A0-9404-611F68EFA37C&sdkversion=1.4.20
request:  GET blob:https://m.amap.com/9bdbb862-8b44-494a-a0e8-86ff992fa1ed
request:  GET blob:https://m.amap.com/a6119d09-0962-47de-96f1-47dcd5fc7705
request:  GET https://g.alicdn.com/alilog/??s/8.15.21/plugin/aplus_client.js,aplus_cplugin/0.7.11/toolkit.js,aplus_cplugin/0.7.11/monitor.js,s/8.15.21/aplus_wap.js,s/8.15.21/plugin/aplus_spmact.js?v=20220926175630
request:  GET https://g.alicdn.com/??/sd/baxia/2.2.3/baxiaCommon.js
request:  GET https://g.alicdn.com/AWSC/fireyejs/1.223.1/fireyejs.js
request:  GET https://m.amap.com/service/combo/?f=core.js@1e866267;vendor.js@2ee99dec;plugin.js@acc676e2;common.js@6172da20;tpl.js@f551753b;component.js@120d739d;base.js@dda90493
request:  GET https://m.amap.com/service/combo/?f=base/pure.css@a9531f16;common.css@814b33c1;common/loading.css@9e2c3f4f
response:  200 blob:https://m.amap.com/9bdbb862-8b44-494a-a0e8-86ff992fa1ed
二、Page.route拦截更改网络请求
1、全部拦截方法
  • route.abort(**kwargs)
  • route.continue_(**kwargs)
  • route.fallback(**kwargs)
  • route.fulfill(**kwargs)
  • route.request
2、拦截所有的图片请求
代码语言:javascript复制
from playwright.sync_api import sync_playwright
import re


def run_route(playwright):
    """
    拦截所有的图片请求
    :param playwright:
    :return:
    """
    chromium = playwright.chromium  # or "firefox" or "webkit".

    # 手机设备设置为iPhone 12 Pro Max
    iphone_12 = playwright.devices['iPhone 12']
    # 启动谷歌有窗口浏览器
    browser = chromium.launch(headless=False)
    # 配置用户定位信息
    context = browser.new_context(
        **iphone_12,
        locale='zh-CN',
        geolocation={'longitude': 116.39014, 'latitude': 39.913904},
        permissions=['geolocation']
    )

    page = context.new_page()
    # route 的参数默认是通配符,也可以传递编译好的正则表达式对象
    page.route("**/*.{png,jpg,jpeg}", lambda route: route.abort())
    page.route(re.compile(r"(.png$)|(.jpg$)"), lambda route: route.abort())

    # 监听请求事件
    page.on("request", lambda request: print("request: ", request.method, request.url))
    # 监听响应事件
    page.on("response", lambda response: print("response: ", response.status, response.url))
    page.goto("https://m.amap.com/", wait_until='domcontentloaded')
    browser.close()

仍然以高德H5网页为例,拦截图片请求后,可看到运行结果中无图片请求返回结果

代码语言:javascript复制
request:  GET https://m.amap.com/
response:  200 https://m.amap.com/
request:  GET https://a.amap.com/pc/static/config/config_m/lbs_m_about.js
request:  GET https://g.alicdn.com/aes/??tracker/1.0.34/index.js,tracker-plugin-jserror/1.0.13/index.js,tracker-plugin-api/1.1.11/index.js,tracker-plugin-perf/1.1.6/index.js,tracker-plugin-longtask/1.0.1/index.js,tracker-plugin-pv/2.4.4/index.js,tracker-plugin-event/1.2.5/index.js
response:  200 https://a.amap.com/pc/static/config/config_m/lbs_m_about.js
request:  GET https://webapi.amap.com/maps?v=1.4.15&key=ab403a9b356e63c917ff05b037cd3e7d&plugin=AMap.Scale
response:  200 https://g.alicdn.com/aes/??tracker/1.0.34/index.js,tracker-plugin-jserror/1.0.13/index.js,tracker-plugin-api/1.1.11/index.js,tracker-plugin-perf/1.1.6/index.js,tracker-plugin-longtask/1.0.1/index.js,tracker-plugin-pv/2.4.4/index.js,tracker-plugin-event/1.2.5/index.js
request:  GET https://g.alicdn.com/alilog/mlog/aplus_wap.js
request:  GET https://g.alicdn.com/??/AWSC/AWSC/awsc.js,/sd/baxia-entry/baxiaCommon.js
request:  GET https://img.yuanmabao.com/zijie/pic/2022/12/05/h3bisroaxct.png
response:  200 https://webapi.amap.com/maps?v=1.4.15&key=ab403a9b356e63c917ff05b037cd3e7d&plugin=AMap.Scale
response:  200 https://g.alicdn.com/alilog/mlog/aplus_wap.js
response:  200 https://g.alicdn.com/??/AWSC/AWSC/awsc.js,/sd/baxia-entry/baxiaCommon.js
request:  GET https://webapi.amap.com/maps/modules?v=1.4.20&key=ab403a9b356e63c917ff05b037cd3e7d&vrs=1656665904502&m=touch,vectorlayer,overlay,wgl,AMap.Scale,sync
response:  200 https://webapi.amap.com/maps/modules?v=1.4.20&key=ab403a9b356e63c917ff05b037cd3e7d&vrs=1656665904502&m=touch,vectorlayer,overlay,wgl,AMap.Scale,sync
request:  GET https://m.amap.com/_AMapService/v3/log/init?s=rsv3&product=JsInit&key=ab403a9b356e63c917ff05b037cd3e7d&t=1664354421103&resolution=390*664&mob=1&vt=1&dpr=3&scale=1&detect=true&callback=jsonp_347588_&platform=JS&logversion=2.0&appname=https://m.amap.com/&csid=D889FEBD-1BB4-49DC-8BFE-BFA8A8DB53E3&sdkversion=1.4.20
request:  GET https://g.alicdn.com/alilog/??s/8.15.21/plugin/aplus_client.js,aplus_cplugin/0.7.11/toolkit.js,aplus_cplugin/0.7.11/monitor.js,s/8.15.21/aplus_wap.js,s/8.15.21/plugin/aplus_spmact.js?v=20220926175630
request:  GET https://g.alicdn.com/??/sd/baxia/2.2.3/baxiaCommon.js
request:  GET https://g.alicdn.com/AWSC/fireyejs/1.223.1/fireyejs.js
request:  GET https://m.amap.com/service/combo/?f=core.js@1e866267;vendor.js@2ee99dec;plugin.js@acc676e2;common.js@6172da20;tpl.js@f551753b;component.js@120d739d;base.js@dda90493
request:  GET https://m.amap.com/service/combo/?f=base/pure.css@a9531f16;common.css

0 人点赞