我需要抓取/废弃静态非结构化 HTML,我正在尝试使用 Node.js 代码获取内容,我尝试使用 Cheerio 和 xpath 失败。
http://static.puertos.es/pred_simplificada/Predolas/Tablas/Cnt/PAS.html
要获取的第一个元素的 Xpath 是/html/body/center/center/table/tbody/tr[3],然后我需要获取 TR 中的每个 TD 文本。
如果尝试获取tbody Node
var parser = new parse5.Parser();
var document = parser.parse(response.toString());
var xhtml = xmlser.serializeToString(document);
var doc = new dom().parseFromString(xhtml);
var select = xpath.useNamespaces({"x": "http://www.w3.org/1999/xhtml"});
var nodes = select("//x:tbody", doc);
我总是收到一个[]
Node 。
使用cheerio,我尝试迭代TR元素,但正如我上面提到的,没有成功。
var $ = cheerio.load(response);
$('tr').each(function(i, e) {
console.log("Content %j", $(e));
});
最佳答案
这表明 Cheerio 无法在非结构化且没有 CSS HTML 的情况下正常工作。因此,我在 that tutorial 之后使用 YQL 尝试了另一种解决方法
select * from html where url='http://static.puertos.es/pred_simplificada/Predolas/Tablas/Cnt/PAS.html' and xpath='//html/body/center/center/table/tbody'
通过 yql,我得到了我所需要的,所以我将集成它 node-yql
关于node.js - 使用nodejs对非结构化html进行爬虫,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33466777/