问题
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