puppeteer:如DevTools的广告网络标签中所示,访问特定请求的JSON响应 [英] puppeteer: Access JSON response of a specific request as in the network tab of DevTools

查看:132
本文介绍了puppeteer:如DevTools的广告网络标签中所示,访问特定请求的JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想直接获取屏幕快照中显示的最后一个HTTP请求的响应.

I'd like to directly get the response of the last HTTP request shown in the screenshot.

当前的伪造者代码如下所示.有人可以告诉我如何修改它,以便它将直接从浏览器获取JSON响应吗?谢谢.

The current puppeteer code is shown below. Could anybody show me how to modify it so that it will get the JSON response directly from the browser? Thanks.

const puppeteer = require('puppeteer');

(async () => {
    //  const browser = await puppeteer.launch();
    const browser = await puppeteer.launch({
        headless: false
        , args: ['--user-agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3312.0 Safari/537.36"']
    });
    const page = await browser.newPage();
    await page.goto('https://www.genecards.org/cgi-bin/carddisp.pl?gene=BSCL2');
    await page.goto('https://www.genecards.org/cgi-bin/carddisp.pl?gene=BSCL2');

    const linkHandlers = await page.$x("//div[@id='enhancers']//a[@data-track-event='Table See-All']");
    if (linkHandlers.length > 0) {
        await linkHandlers[0].click();
    } else {
        throw new Error("Link not found");
    }

    const html = await page.content()
    //await browser.close();
    console.log(html)
})();

推荐答案

您可以使用

You can use page.waitForResponse to wait for the response and response.json to parse the response as JSON.

代码

以此替换await linkHandlers[0].click();部分:

const [response] = await Promise.all([
    page.waitForResponse(response => response.url().includes('/gene/api/data/Enhancers')),
    linkHandlers[0].click()
]);
const dataObj = await response.json();
console.log(dataObj);

这将首先等待响应(同时进行单击).检测到响应后,将响应解析为JSON.要获得纯文本形式的响应结果(而不是解析它),可以使用 response.text()

This will first wait for the response (while in parallel making the click). After the response is detected the response is parsed as JSON. To get the response result as plain text (instead of parsing it), you can use response.text()

这篇关于puppeteer:如DevTools的广告网络标签中所示,访问特定请求的JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆