我试图在下面的HTML中访问的项目是"GMV DLL VERSION2“
<div class="container content">
<main>
<h2 id="rpcs--gmv-dll-version"><a href="/artifacts/vistaRPC%20documentation/TableOfContent">RPCs</a> → GMV DLL VERSION</h2>
<h3 id="vista-file-8994">VISTA File 8994</h3>
<table>
<thead>
<tr>
<th>property</th>
<th>value</th>
</tr>
</thead>
<tbody>
<tr>
<td>label</td>
<td>GMV DLL VERSION2</td>我正在尝试抓取这个网站(http://vistadataproject.info/artifacts/vistaRPC%20documentation/GMV%20DLL%20VERSION)
并将其输出为文本文件。我成功地使用reddit.com进行了一次测试。然而,我似乎不能让这个页面上的任何元素。为了测试它,甚至在处理表格之前,我就一直在尝试抓取页面早期(顶部区域)出现的一些元素。
表格中缺少classNames和Id已经够棘手的了,但是连标题文本都得不到,这真的让我想知道这是怎么回事。任何意见都将不胜感激。请求(http://vistadataproject.info/artifacts/vistaRPC%20documentation/GMV%20DLL%20VERSION,(err,res,body) => {
if (err) {
console.log('Error: ' + err);
}
console.log('Status: ' + res.statusCode);
const $ = cheerio.load(body);
$('header.masthead > div.container').each(( index, tr ) => {
// var children = $(this).children();
const tableData = $(this).find('a.logo').text();
console.log("Table Contents: " + tableData);
fs.appendFileSync('test.txt', tableData + '\n' + 'Captured');
});发布于 2017-04-07 08:15:29
问题是'masthead‘是一个类名,而不是id。“container”和“logo”也是如此。因此,您需要相应地调整选择器:
$('header.masthead > div.container').each(( index, tr ) => {但是,这只能得到标题信息,不包括包含“property => value”数据的表。对于该信息,您只需查找'‘标记下的子表。
https://stackoverflow.com/questions/43267665
复制相似问题