我需要函数scrape
返回在page.on("request")
事件处理程序中获得的值。
async function scrape(url) {
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on("request", async(request) => {
return "fish"
}
await page.goto(url)
}
当前:
const ans = await scrape(url)
console.log(ans)
"undefined'
预期:
const ans = await scrape(url)
console.log(ans)
"fish"
答案 0 :(得分:1)
您需要返回在看到等待事件时解决的承诺
const matchRequest = request => request.method() === 'GET'; // your filter
async function scrape(url) {
return new Promise(resolve => {
const page = await browser.newPage();
// not sure what your logic is, but if you don't need to cancel or modify requests/resposes you probably don't need interception
// await page.setRequestInterception(true);
page.on("response", async(response) => {
if (matchRequest(response.request())) {
resolve(response.buffer());
}
}
await page.goto(url);
})
}
const body = await scrape('https://example.com');
答案 1 :(得分:0)
尝试按照以下步骤操作:
async function scrape(url) {
let sendRequest = []
const browser = await puppeteer.launch()
const page = await browser.newPage()
await page.setRequestInterception(true)
page.on('request', request => {
request.continue()
sendRequest.push('fish')
})
await page.goto(url)
return sendRequest
}
使用request.continue()
继续请求,并带有可选的请求替代。要使用此功能,应使用page.setRequestInterception
启用请求拦截。如果未启用请求拦截,则会立即引发异常。