简体   繁体   English

如何将 HTML 表格对象转换为字符串?

[英]How do I convert a HTML table object to a string?

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.我正在从网页上抓取一些 HTML,我正在尝试从页面上抓取一个表格元素并将其转换为 JSON。

I found a node js library that does this but it requires a string as an argument.我找到了一个执行此操作的节点 js 库,但它需要一个字符串作为参数。 How do I turn the HTML object into a string?如何将 HTML 对象转换为字符串? When I call the toString() function on it, it returns:当我对其调用 toString() 函数时,它返回:

"[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)

To get all the HTML - including the element in question - as a string change:获取所有HTML - 包括相关元素 - 作为字符串更改:

componentTable.toString()

To:到:

componentTable.outerHTML

Just converting a HTMLElement to a string with toString() will give the object name, like you have seen.正如您所见,只需使用toString()将 HTMLElement 转换为字符串就会给出对象名称。

You probably want to use innerHTML你可能想使用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. return componentTable.innerHTML将为您提供该表中的 html 字符串,这取决于您找到的节点库,可能足够也可能不够。

It's worth looking through the docs of that library, as they probably have an example for doing this.值得查看该库的文档,因为他们可能有这样做的示例。

你要使用innerHtml,这是关键

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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