Node.js puppeteer - Fetching content seperated by br and store items in seperate variables

不想你离开。 提交于 2019-12-23 23:11:59

问题


I'm using node.js and puppeteer to get some data. How can I save the content of an element (which is divided by line break <br>) in two separate variables?

That's the HTML I'm looking at:

<table summary="">
    <tbody>
        <tr nowrap="nowrap" valign="top" align="left">
            <td nowrap="nowrap">2018-08-14<br>16:35:41</td>
        </tr>
    </tbody>
</table>

I'm getting the content of the td like this (app.js):

let tableCell04;
let accepted;

tableCell04 = await page.$( 'body div table tr td' );
accepted = await page.evaluate( tableCell04 => tableCell04.innerText, tableCell04 );

console.log('Accepted: '+accepted);

The output in console is:

Accepted: 2018-08-14
16:35:41

But what I would like to have is storing the content which is separated by the line break in two separate variables so that I get sth like this:

Accepted_date: 2018-08-14
Accepted_time: 16:35:41

回答1:


Hi you can use tableCell04.innerHTML to get the html instead of the plain text.

accepted = await page.evaluate( tableCell04 => tableCell04.innerHTML, tableCell04 );

const [Accepted_date, Accepted_time] = accepted.split('<br>');


来源:https://stackoverflow.com/questions/51855508/node-js-puppeteer-fetching-content-seperated-by-br-and-store-items-in-seperate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!