如何使用 Puppeteer 从 XHR 请求中获取 body/json 响应 [英] How to get body / json response from XHR request with Puppeteer

查看:225
本文介绍了如何使用 Puppeteer 从 XHR 请求中获取 body/json 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我用 Puppeteer 抓取的网站获取 JSON 数据,但我不知道如何取回请求的正文.这是我尝试过的:

I want to get the JSON data from a website I'm scraping with Puppeteer, but I can't figure how to get the body of the request back. Here's what I've tried:

const puppeteer = require('puppeteer')
const results = [];
(async () => {
    const browser = await puppeteer.launch({
        headless: false
    })
    const page = await browser.newPage()
    await page.goto("https://capuk.org/i-want-help/courses/cap-money-course/introduction", {
        waitUntil: 'networkidle2'
    });

    await page.type('#search-form > input[type="text"]', 'bd14ew')  
    await page.click('#search-form > input[type="submit"]')

    await page.on('response', response => {    
        if (response.url() == "https://capuk.org/ajax_search/capmoneycourses"){
            console.log('XHR response received'); 
            console.log(response.json()); 
        } 
    }); 
})()

这只是返回一个承诺挂起的函数.任何帮助都会很棒.

This just returns a promise pending function. Any help would be great.

推荐答案

由于 response.json 返回一个 promise,我们需要等待它.

As response.json returns a promise we need to await it.

page.on('response', async (response) => {    
    if (response.url() == "https://capuk.org/ajax_search/capmoneycourses"){
        console.log('XHR response received'); 
        console.log(await response.json()); 
    } 
}); 

这篇关于如何使用 Puppeteer 从 XHR 请求中获取 body/json 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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