简体   繁体   中英

How to scrape a table using Puppeteer?

What I want is to scrape a table, and save the data in an Array like result=[tr][ts] .

How can I make the result I want?

I'm trying something like this:

const row = await mainframe.$$eval('#clasificacion > .panel > .table- responsive > #resultadosTable > tbody > tr ', trs => trs.map((tr) =>{
console.log(tr);
return tr.textContent;}));

But the result is like result=[tr] .

You can use the following method to obtain a 2D array of table cell textContent which can be accessed via row[rowNum][cellNum] :

const selector = '#clasificacion > .panel > .table-responsive > #resultadosTable > tbody > tr';

const row = await mainframe.$$eval(selector, trs => trs.map(tr => {
    const tds = [...tr.getElementsByTagName('td')];
    return tds.map(td => td.textContent);
}));

// console.log(row[rowNum][cellNum]); <-- textContent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM