Puppeteer js仅返回html标头,而完整的html可以在chrome的dev工具中看到

我正在我的node js应用程序中使用puppeteer js来抓取一个歌词网站,该URL指向查询结果(通过url查询),例如:

https://shironet.mako.co.il/search?q=fire

在此示例中,查询词为“ fire”。

问题在于,我认为该网站是使用一些spa框架构建的,因为无论我如何尝试获取HTML,我都只会获得包含一些压缩js函数的标头和空的html正文。 / p>

在我的devtools中检查页面时,我看到了正常的HTML。

这是剪贴代码:

'use strict'
const pup = require('puppeteer'),cherrio = require('cheerio'),baseUrl = 'https://shironet.mako.co.il/search?q=';

async function findInitialData(songName){
    if(!songName){
        return 'no song name to scrap'
    }
    console.log(`start findInitialData with songName: ${songName}`)

    console.time('initial-scrap')
    pup.launch({
        headless: true
    }).then(
        async browser =>{
            let final = []
            const page = await browser.newPage()
            console.log(`there is a page`)

            await page.goto(`${baseUrl}/${songName}`),{waitUntil:'networkidle2'}
            await page.waitFor(10 * 1000);
            const html = await page.content()
            console.log(`html: `,html)

            const $ = cherrio.load(html)
            $('a.search_link_name_big').each((index,val)=>{
                console.log(`val: `,val)

                let text =  $(value).text().replace(/[\n\t]/gi,'')
                let link = $(value).attr('href')
                if(index%2==1){
                    obj = {}
                    obj["singer"]=text
                    final.push(obj)
                }
                else{
                    obj['link']= link
                    obj['song'] = text
                }
                console.log(`final: `,final)
                browser.close()
                setTimeout(() => {
                    console.timeEnd('initial-scrap')
                    return final
                },3000);

            })
        }
    )
} 

module.exports = {findInitialData}

当我使用headless:false选项时,我在devtools中看到主体为空(并且标头填充有相同的函数)并且页面根本没有加载。

这是我得到的一些回应,包括无头和无头:

<html><head><meta charset="utf-8"><script>function i700(){}i700.F20=function (){return typeof i700.O20.p60==='function'?i700.O20.p60.apply(i700.O20,arguments):i700.O20.p60;};i700.X70=function (){return typeof i700.v70.p60==='function'?i700.v70.p60.apply(i700.v70,arguments):i700.v70.p60;};i700.Z20=function (){return typeof i700.O20.P20==='function'?i700.O20.P20.apply(i700.O20,arguments):i700.O20.P20;};i700.Q60=function (){return typeof i700.Y60.P20==='function'?
...
;winsocks();</script></head><body></body></html>

devtools中显示的一些HTML:

    <tbody><tr>
        <td class="global_main_shadow" align="center">
            <table width="1020" cellspacing="0" cellpadding="0" border="0">
                <tbody><tr>
                    <td width="20" valign="top" align="left">

                <img src="/jsp/images/global_bg_right.gif" width="20" height="556"></td>
...


</tbody>

我在做什么错了?

由于没有任何主体,cheerio部分将失效,并且该功能无法正常工作。

我在这里和google中看到了一些解决类似问题的答案,但是对于他们,page.waitFor和添加waitUntil networkidle2解决了它,但对我来说不是。

编辑:

我尝试使用axios和失眠之类的工具发送对相同URL的请求,但它们得到的响应为空。

但是当我使用邮递员时,我得到了正确的HTML。

哪个邮递员对其他工具做错的事情是正确的?

任何帮助将不胜感激!

nidi_1 回答:Puppeteer js仅返回html标头,而完整的html可以在chrome的dev工具中看到

{
  "CacheStatistics_ReadHits": 0,"CacheStatistics_ControllerPrefetchRemaining": [0,0],"CacheStatistics_ControllerReadMisses": [0,0]
}

它在我的机器上工作。

enter image description here

enter image description here

本文链接:https://www.f2er.com/3159360.html

大家都在问