Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在所有脚本和页面加载完成后获取所有html数据?(木偶演员)_Javascript_Node.js_Parsing_Web Scraping_Puppeteer - Fatal编程技术网

Javascript 如何在所有脚本和页面加载完成后获取所有html数据?(木偶演员)

Javascript 如何在所有脚本和页面加载完成后获取所有html数据?(木偶演员),javascript,node.js,parsing,web-scraping,puppeteer,Javascript,Node.js,Parsing,Web Scraping,Puppeteer,最后我想出了如何使用Node.js。已安装所有库/扩展。因此,木偶演员正在工作,但与之前使用Xmlhttp时一样。。。它只获取页面的模板/正文,没有需要的信息。页面上的所有脚本在浏览器(Web app?)中打开几秒钟后就开始工作。我需要在整个页面加载后在某些标签中获取信息。另外,我会问,是否可以使用纯JavaScript,因为我不使用类似jQuery的代码。所以对我来说难度加倍了 这是我到目前为止的资料 const puppeteer = require('puppeteer'); const

最后我想出了如何使用Node.js。已安装所有库/扩展。因此,木偶演员正在工作,但与之前使用Xmlhttp时一样。。。它只获取页面的模板/正文,没有需要的信息。页面上的所有脚本在浏览器(Web app?)中打开几秒钟后就开始工作。我需要在整个页面加载后在某些标签中获取信息。另外,我会问,是否可以使用纯JavaScript,因为我不使用类似jQuery的代码。所以对我来说难度加倍了

这是我到目前为止的资料

const puppeteer = require('puppeteer');
const $ = require('cheerio');
let browser;
let page;

const url = "really long link with latitude and attitude";

(async () => puppeteer
  .launch()
  .then(await function(browser) {
    return browser.newPage();
})
  .then(await function(page) {
    return page.goto(url).then(function() {
      return page.content();
    });
  })
  .then(await function(html) {
    $('strong', html).each(function() {
      console.log($(this).text());
    });
  })
  .catch(function(err) {
    //handle error
  }))();
我只在强标记中获得模板默认的主体元素。但它应该包含比10项多得多的数据。

一些注意事项:

  • 您不需要使用
    puppeter
    进行
    cheerio
    ,也不需要重新分析
    page.content()
    :您已经拥有运行所有脚本的完整DOM,您可以像在浏览器中一样,使用web API上下文和Node.js API上下文并在两者之间传输可序列化数据,对
    窗口中的任何代码进行求值

  • 尝试仅使用async/await,这将简化代码和流程

  • 如果需要等待所有脚本和其他依赖项加载完毕,请在中使用
    waitill:'networkidle0'

  • 如果您怀疑文档脚本需要一些时间才能达到所需的状态,请使用各种测试函数,如或fall back to

  • 下面是一个输出页面中所有标记名的简单脚本

    “严格使用”;
    const puppeter=require('puppeter');
    (异步函数main(){
    试一试{
    const browser=wait puppeter.launch();
    const[page]=wait browser.pages();
    等待页面。转到('https://example.org/“,{waitUntil:'networkidle0'});
    常量数据=等待页面。评估(
    ()=>Array.from(document.querySelectorAll('*'))
    .map(elem=>elem.tagName)
    );
    控制台日志(数据);
    等待浏览器关闭();
    }捕捉(错误){
    控制台错误(err);
    }
    })();
    
    您可以更详细地指定任务,我们可以尝试编写更合适的内容


    www.bezrealitky.cz的脚本(以下评论中的任务):

    “严格使用”;
    常数fs=要求('fs');
    const puppeter=require('puppeter');
    (异步函数main(){
    试一试{
    const browser=wait puppeter.launch();
    const[page]=wait browser.pages();
    page.setDefaultTimeout(0);
    等待page.goto('https://www.bezrealitky.cz/vyhledat?offerType=pronajem&estateType=byt&disposition=&ownership=&construction=&equipped=&balcony=&order=timeOrder_desc&boundary=%5B%5B%7B%22lat%22%3A50.171436864513%2C%22lng%22%3A14.506905276796942%7D%2C%7B%22lat%22%3A50.154133576294%2C%22lng%22%3A14.599004629591036%7D%2C%7B%22lat%22%3A50.14524430128%2C%22lng%22%3A1417.29297 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7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%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2 2C%22lng%22%3A14.337635637038034%22%22%22%22%22%22%7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 22lng%22%3A14.29483886971002%7D%2C%7B%22%22%22%22%3,22%22%3,22%3,22%22%3,18.315872828525252525252525252527 7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%19107936%7D%2C%7B%22lat%18.7878534949494939393939393939393939393939393939393939393939393939393939393939393939393939397%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%5276796942%7D%5D%5D和Hasdawnbou2.28944704707777777%22%22%22%22%22%22%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%7%2%2%2%2%2%2%2%2%2%7%7%7%7%7%7%7%7%7%7%7%7%7%7%7%22%7%7%7%7%7%7%7%22%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%2%50.289447077141126%2C%22lng%22%3A14.68724263943227%7D%5D%5D¢er=%7B%22lat%22%3A50.16447196305031%2C%22lng%22%3A14.3875218752125%7D&zoom=11&locationInput=praha&limit=15’;
    wait page.waitForSelector(“#搜索内容按钮.btn图标”);
    while(等待页面。$(“#搜索内容按钮.btn图标”)!==null){
    const articlesForNow=(等待页面。$$(“#搜索内容文章”)。长度;
    log(`Articles for now:${articlesForNow}.get more…`);
    等待承诺([
    第页,评估(
    ()=>{document.querySelector(“#搜索内容按钮.btn图标”)。单击();}
    ),
    page.waitForFunction(
    old=>document.querySelectorAll(“#搜索内容文章”).length>old,
    
        const puppeteer = require('puppeteer');
    
        (async function main() {
          try {
            const browser = await puppeteer.launch();
            const [page] = await browser.pages();
    
            await page.goto('https://example.org/', { waitUntil: 'networkidle0' });
            const data = await page.evaluate(() => document.querySelector('*').outerHTML);
    
            console.log(data);
    
            await browser.close();
          } catch (err) {
            console.error(err);
          }
        })();