【发布时间】:2018-05-29 14:34:16
【问题描述】:
我在将 html 表解析为 json 时遇到问题。
Htlm 表格页面:
<div id="content">
<h1>content-information</h1>
<table class="testinformation">
<thead>
<tr>
<th>hello</th>
<th>test_text</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://example.com">hello1</a></td>
<td><a href="https://example.com/test_text">test_text</a></td>
</tr>
<tr>
<td><a href="https://example.com">hello2</a></td>
<td><a href="https://example.com/test_text2">test_text2</a></td>
</tr>
</tbody>
</table>
</div>
Node js/cheerio 脚本,它不能正常工作:
var cheerio = require('cheerio'),
cheerioTableparser = require('cheerio-tableparser');
const request = require('request');
request('https://correct-url.com', function (error, response, html) {
if (!error) {
const $ = cheerio.load(html)
cheerioTableparser($);
var data = $("testinformation").parsetable();
console.log(data);
}
})
但是响应是空的。
【问题讨论】:
-
你检查你的
html变量了吗?它真的包含你需要的html吗?另外,我认为你应该在使用选择器时在类名中添加点——比如$(".testinformation") -
@profiler 你试过 $(".testinformation") 吗?正如jehy所说
-
结果也是空的 - [].
-
@Jehy,你有什么想法吗?
-
抱歉,我没有想法,需要更多数据 - 例如,源数据或 html。
标签: javascript html node.js parsing cheerio