node.js - Node.js Cheerio 返回空且没有错误
问题描述
我正在尝试从具有如下结构的表中获取数据:
<table id="ros_table" class="info" style="display: none;">
<tr>
<th>First</th>
<th>Second</th>
<th>Third</th>
<th>Forth</th>
<th>Fifth</th>
<th>Six</th>
</tr>
<tr>
<td style="white-space: nowrap;"><a href="#">120241</a></td>
<td style="white-space: nowrap;"><a href="#">69801:001:0255</a></td>
<td>Name</td>
<td>Name 2</td>
<td><span style="white-space: nowrap;">90400 m<sup>2</sup></span> <span style="white-space: nowrap;">(9.04 ha)</span></td>
<td style="white-space: nowrap;">jah</td>
</tr>
我使用的代码是这样的:
fetchData(url).then( (res) => {
const html = res.data;
const $ = cheerio.load(html);
const statsTable = $('.table#ros_table > tr');
statsTable.each(function() {
let title = $(this).find('td').text();
console.log(title);
});
})
async function fetchData(url){
console.log("Looking for stuff you need...")
// Make the call
let response = await axios(url).catch((err) => console.log(err));
if(response.status !== 200){
console.log("Blah, this did not work out");
return;
}
return response;
}
只需一个简单的 que,它就可以正常工作,但由于某种原因,我可以让它在这张桌子上工作。
解决方案
你需要return
从每个.then()
块中取出一些东西,我假设你想要这样的东西?:
fetchData(url).then( (res) => {
const html = res.data;
const $ = cheerio.load(html);
const statsTable = $('.table#ros_table > tr');
return statsTable.map(function() {
return $(this).find('td').text();
});
})
推荐阅读
- django - Sorl-Thumbnail 中的 URL 序列化性能问题
- string - 检查文件中是否存在多行字符串
- firebase - 如何找到图像路径?
- python - Selenium - 访问离线网站后如何继续使用 url 列表?
- javascript - 在 NODEJS 中批量合并图像
- deployment - 使用 Actions 和 FTP Deploy 部署更大文件库的 GitHub
- javascript - 图片未显示在表单提交上的文本上
- python - 选择多列并分组
- c# - 在 Unity 中禁用脚本时出现 CS1061 错误
- sql - 如何使用 Oracle JSON_OBJECT 和 JSON_ARRAYAGG 从多个列表中聚合不同的值