javascript - 尝试使用 Nodejs 从 HTML 响应中提取信息
问题描述
我正在尝试使用cheerio 和puppeteer 模块从我的HTML 响应中提取电子邮件(myemail@hotmail.com)。但我得到了不同的东西,我根本不需要使用它们。它位于 td/tr 的 p2 类中。同时将 tr 作为参数
这就是我的代码的样子:
const puppeteer = require('puppeteer');
const $ = require('cheerio');
const url = 'https://mywebsite.com';
puppeteer
.launch()
.then(function(browser) {
return browser.newPage();
})
.then(function(page) {
return page.goto(url).then(function() {
return page.content();
});
})
.then(function(html) {
$('tr', html).each(function() {
// putting all the result into the list
console.log($(this).text());
});
})
.catch(function(err) {
//handle error
});
我得到这个输出:
移动邮箱电路
myemail@hotmail.com
邮箱 myemail@hotmail.com 经理秘书我只需要 myemail@hotmail.com
这是我的 HTML 表:
</td>
</tr>
<tr>
<td class="p1">E-mail</td>
<td class="p2">
<span style="float: none; word-wrap: break-word;"> <a href="mailto:myEmal@hotmail.com"> myEmal@hotmail.com
<div style="padding-right: 2px; background-position: -115px -434px; height: 14px !important; float: right" class="ico"></div>
</a>
</span>
</td>
解决方案
尝试在该类的 td 中获取内容。
console.log($(this).find('td.p2').text());
推荐阅读
- javascript - 我需要帮助从
带有javascript的元素
- java - Java Swing GUI JLabel不显示
- ios - Nativescript 应用程序可以在模拟器上运行,但不能在真实设备上运行。崩溃报告说 EXC_BAD_ACCESS (SIGSEGV)
- php - 循环多维数组以输出 uniq 数字列表
- google-apps-script - 如何将文件内容从 gdoc 更新到 docx
- node.js - 当我运行“节点--版本”时,我收到此消息“zsh:找不到命令:节点”
- redux - react-admin 注销时出错
- java - 我如何在 Java 的 Jackson 库中打印对象名称作为 JSON 的一部分?
- sql - 在 DAX 或 Python 中为自己的组创建显示另一列的不同计数的列
- r - R未标记的动态模型 - 对比错误