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)
})();
您可以使用 response.json
将响应解析为JSON.
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()