1

我一直在尝试制作一个程序,该程序可以在用户浏览站点(并执行请求)时主动拦截请求并返回这些请求的响应正文。

当前的代码似乎只在进入站点时截取请求的详细信息,而不会在用户在站点上执行操作时继续打印请求的详细信息。

我真的很感激任何帮助!

这是我目前的整个程序:

import asyncio
import json
import time
import os
from pyppeteer import launch
from datetime import datetime

args = [' --window-size=500,645']

async def intercept_network_response(response):
    # In this example, we care only about responses returning JSONs
    if "application/json" in response.headers.get("content-type", ""):
        # Print some info about the responses
        print("--------------------------------------")
        print("RESPONSE:", response)
        print("URL:", response.url)
        print("Method:", response.request.method)
        print("Response headers:", response.headers)
        print("Request Headers:", response.request.headers)
        print("Response status:", response.status)
        # Print the content of the response
        try:
            # await response.json() returns the response as Python object
            print("Content: ", await response.json())
            print("--------------------------------------")
        except json.decoder.JSONDecodeError:
            # NOTE: Use await response.text() if you want to get raw response text
            print("Failed to decode JSON from", await response.text())


async def main():
    print('Launching')
    browser = await launch(headless=False, autoclose=False, args=args)
    print('Opening')
    page = await browser.newPage()
    await page.setViewport({
        'width': 500,
        'height': 512
    })
    page.on('response', intercept_network_response)
    print('Going to site URL')
    await page.goto('https://kith.com/checkpoint')
    time.sleep(10000)
    await browser.close()


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

0 回答 0