【发布时间】:2019-06-08 11:12:53
【问题描述】:
我正在尝试使用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>
【问题讨论】:
-
可以使用正则表达式吗?它看起来是一个不错的解决方案。
-
HTML 看起来不像实际的 HTML,因为它的任何地方都没有
Manager Secretary。 -
@Keith 这是 html 表的一部分
-
确实,感兴趣的部分已被删除.. :)
-
@while 试图把 tr 响应发送给我整个 html 的所有 tr
标签: javascript html node.js puppeteer cheerio