簡體   English   中英

如何使用 Puppeteer 從 XHR 請求中獲取 body / json 響應

[英]How to get body / json response from XHR request with Puppeteer

我想從我用 Puppeteer 抓取的網站獲取 JSON 數據,但我不知道如何取回請求的正文。 這是我嘗試過的:

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()); 
        } 
    }); 
})()

這只是返回一個承諾掛起函數。 任何幫助都會很棒。

response.json返回一個 promise 時,我們需要等待它。

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

你能幫我提建議嗎? 如何提取所有對數組的json響應並從此函數中提取出來?

await page.on('response', async (response) => {    
    if (response.url()){
        console.log('XHR response received'); 

        let rest = await response.json();
    } 
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM