当没有“下一页”按钮而是一堆“页码”页面时的分页

拉米罗

我很高兴用R进行报废,但发现了它的局限性。在试图取消阿根廷最高法院案件摘要时,我发现了一个我找不到答案的问题。很可能的结果做中学---所以,请不要在我的代码工作,但下面一个相当不好的做法,指出。无论如何,我设法:

  1. 访问搜索页面。
  2. 在中输入相关的分类法术语(例如'DECRETO DE NECESIDAD Y URGENCIA')#voces,单击“搜索”并将其剪贴,然后.datosSumarios找到需要的信息(案例名称,日期,报告者等)。代码如下:

const puppeteer = require('puppeteer');

let scrape = async () => {
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();

    await page.goto('https://sjconsulta.csjn.gov.ar/sjconsulta/');

  // wait until element ready  
    await Promise.all([
        page.type('#voces', 'DECRETO DE NECESIDAD Y URGENCIA'),
        page.waitForSelector('.ui-menu-item')
    ]);

    await page.click('.ui-menu-item');

    await Promise.all([
    page.click('.glyphicon-search'),
    page.waitForNavigation({ waitUntil: 'networkidle0' }),
  ]);

    //Here we are in the place we want to be, and then capture what we need:     
    
    const result = await page.evaluate(() => {

        let data = []; // Create an empty array that will store our data
        
        let elements = document.querySelectorAll('.row'); // Select all Products

        for (var element of elements){ // Loop through each proudct
            
            let title = document.querySelector('.datosSumario').innerText;

            data.push({title}); // Push an object with the data onto our array

        }

        return data; // Return our data array
        
    });

    //review -> 
    
    await page.click('#paginate_button2')  

    browser.close();
    return result;
};

scrape().then((value) => {
    console.log(value); // Success!
});

我似乎无法做的是浏览不同的页面。如果您跟随该页面,您会发现分页非常奇怪:没有“下一页”按钮,而是一堆“页面编号按钮”,我可以按此按钮,但不能重复上面代码的打包部分。我尝试了一个循环功能(无法使其正常工作)。我已经阅读了一些分页教程,但是找不到面对这种特殊问题的教程。

#更新

我能够解决分页问题,​​但目前看来,我似乎无法提供一种功能来实际分页需要在分页内工作的文本(它可以在单个页面外运行)。分享,以防有人指出我可能犯的明显错误。

const puppeteer = require('puppeteer');
const fs = require('fs');

let scrape = async () => {
    const browser = await puppeteer.launch({headless: false});
    const page = await browser.newPage();

    await page.goto('https://sjconsulta.csjn.gov.ar/sjconsulta/');

  // wait until element ready  
    await Promise.all([
        page.type('#voces', 'DECRETO DE NECESIDAD Y URGENCIA'),
        page.waitForSelector('.ui-menu-item')
    ]);

    await page.click('.ui-menu-item');

    await Promise.all([
    page.click('.glyphicon-search'),
    page.waitForNavigation({ waitUntil: 'networkidle0' }),
  ]);

    var results = []; // variable to hold the "sumarios" I need
    var lastPageNumber = 2; // I am using 2 to test, but I can choose any number and it works (in this case, the 31 pages I need to scrap)
    for (let index = 0; index < lastPageNumber; index++) {
        // wait 1 sec for page load
        await page.waitFor(5000);
        // call and wait extractedEvaluateCall and concatenate results every iteration.
        // You can use results.push, but will get collection of collections at the end of iteration
        results = results.concat(await MyFunction); // I call my function but the function does not work, see below 
        if (index != lastPageNumber - 1) {
            await page.click('li.paginate_button.active + li a[onclick]'); //This does the trick 
            await page.waitFor(5000);
        }
    }

    browser.close();
    return results;

};

    async function MyFunction() {
    
        const data = await page.evaluate( () => // This bit works outside of the async function environment and I get the text I need in a single page

            Array.from( 

                document.querySelectorAll('div[class="col-sm-8 col-lg-9 datosSumario"]'), element => element.textContent) 
    
            );

    }

scrape().then((results) => {
    console.log(results); // Success!
    
});
vsemozhebuty

您可以尝试document.querySelector('li.paginate_button.active + li a[onclick]')作为等效的下一页按钮。点击后,您可以等待网址以开头的响应'https://sjconsulta.csjn.gov.ar/sjconsulta/consultaSumarios/paginarSumarios.html?startIndex='

#更新

乍看之下,存在一些问题:

  1. MyFunction不被调用:您需要await MyFunction()而不是await MyFunction

  2. 您需要转移pageMyFunction()范围内:

  results = results.concat(await MyFunction(page));
//...
async function MyFunction(page) {
// ...
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

没有“下一页”按钮且网址未更改时的分页

使用反应 js_在下一页上没有路由按钮,在按钮点击时导航到全新的页面

第一页没有页码

Google Analytics(分析)API分页当前页码和下一页验证

为什么我的分页没有在下一页显示其他数据

PHP的SQLite分页有效的方法转到上一页/下一页

Instagram API分页:下一页

分页的下一页链接

Flask 分页迭代页面防止 SQL 注入并在 HTML 模板中添加下一页按钮

php表中断页面,下一页没有行

没有下一个按钮,只有编号可用时,我如何继续下一页?

显示最后一页上的所有表行以及页面加载时的分页

python beautifulsoup-当下一页没有唯一地址时如何转到下一页

Scrapy没有抓取下一页网址

Yii2分页只有上一个和下一个按钮隐藏页码

单击下一页/上一页或第二页时,codeigniter分页重定向到同一页

无法点击下一页的按钮

表格 - 转到下一页的按钮

当我点击注册按钮时,它应该将我重定向到下一页,但这并没有发生

当移动到下一页时,我的上一页和下一页按钮倾向于上下波动

如何使用Selenium单击下一个按钮时一页一页地解析网页?

带分页的角度材料表:点击下一页时进行休息调用

当我进入下一页时 Umbraco 搜索分页不起作用

如何使搜索结果和分页在单击到下一页时不会消失?

Laravel分页获取下一页

下一页无法打开。分页问题

如果url中有参数,如何设置下一页的正确分页href - django

如何获得有关春季分页的下一页

Microsoft Word 365不会将文本行移至下一页,而是保留在图的页面上