0

I am scraping some HTML off a webpage and I'm trying to scrape off a table element off the page and convert it to JSON.

I found a node js library that does this but it requires a string as an argument. How do I turn the HTML object into a string? When I call the toString() function on it, it returns:

"[object HTMLTableElement]"

My code is :

let data = await page.evaluate(() => {
    componentTable = document.querySelector('table.xs-col-12');
    componentTable = componentTable.toString()
    return{
        componentTable
    }
})
console.log(data)
2
  • 2
    probably you need innerHTML property Commented Aug 24, 2020 at 19:09
  • 1
    I'd recommend outerHTML to get the whole table Commented Aug 24, 2020 at 19:12

3 Answers 3

3

To get all the HTML - including the element in question - as a string change:

componentTable.toString()

To:

componentTable.outerHTML
Sign up to request clarification or add additional context in comments.

2 Comments

can get this result in jquery not javascript?
How about $('<div/>').append( componentTable ).html()? See jQuery: outer html
1

Just converting a HTMLElement to a string with toString() will give the object name, like you have seen.

You probably want to use innerHTML

return componentTable.innerHTML will give you a string of the html that is in that table, which depending on the node library you have found, may or may not be sufficient.

It's worth looking through the docs of that library, as they probably have an example for doing this.

Comments

0

you want to use innerHtml, that is the key

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.