【问题标题】:How to get the information in a list of links puppeteer如何获取链接列表中的信息 puppeteer
【发布时间】:2018-10-30 22:21:18
【问题描述】:

我正在尝试使用 Puppeteer 从显示带有链接的表格的网页中抓取信息。

当您打开一个链接时,会打开一个包含更多信息的模式。

我正在尝试打开所有链接,并获取所有链接中的信息。

这是我的代码:

const puppeteer = require('puppeteer');
const fs = require('fs');
puppeteer.launch({headless: false}).then(async browser => {
    const page = await browser.newPage();
    await page.goto('https://fcylf.es/competiciones');

    const competitionframe = await page.frames().find(f => f.name() === 'iframecombos');

    const button = await competitionframe.$('#formulario > div.centrado > input.btn.btn-danger.boton_envio.btn-lg');
    button.click();

    let mainframe = await page.frames().find(f => f.name() === 'iframebooox');
    await mainframe.waitForSelector('#datos > ul > li:nth-child(3) > a');
    const div = await mainframe.$('#datos > ul > li:nth-child(3) > a');
    div.click();


    await mainframe.waitForSelector('#clasificacion > .panel > .table-responsive > #resultadosTable > tbody > tr > td > div > a');
    const teams = await mainframe.$$('#clasificacion > .panel > .table-responsive > #resultadosTable > tbody > tr > td > div > a ');

    const results = [];
    for(let team of teams){

        team.click();
        await mainframe.waitForSelector('#myModalLabel');
        const name = await mainframe.$eval('#myModalLabel', name => name.textContent );
        results.push(name);

        const closebt = await mainframe.$('#datos > div.equipoModal.modal.fade.in > div > div > div.modal-footer > button');
        if(closebt!=null){
            closebt.click();
        }
    }
    console.log(results);
});

但是当我显示日志时,它总是显示相同的结果。

【问题讨论】:

  • 看起来他们使用这个 id #myModalLabel 大约 31 次。我认为这让你很生气。

标签: javascript web-scraping puppeteer headless-browser


【解决方案1】:

我想你想在设置为 display:block 的 div 中找到#myModalLabel

隐藏模式:

<div class="equipoModal modal fade" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true" style="display: none;">

显示模态:

<div class="equipoModal modal fade in" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="false" style="display: block;">

这一行:

const name = await mainframe.$eval('#myModalLabel', name =&gt; name.textContent );

看起来它是从隐藏的模式中抓取 textContent,而不是显示的。

我想。希望这会有所帮助!

【讨论】:

  • 我解决了包括一个等待模式打开的时间,以及一个等待模式关闭的时间。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 2021-08-22
相关资源
最近更新 更多