尝试使用 Nodejs 从 HTML 响应中提取信息
Trying to extract information from HTML response using Nodejs
我正在尝试使用 cheerio 和 puppeteer 模块从我的 HTML 回复中提取电子邮件 (myemail@hotmail.com)。但我得到了不同的东西,我根本不需要使用它们。
它位于 td/tr 中的 Class 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
});
我得到这个输出:
Mobile Post box Circuit
myemail@hotmail.com
E-mail myemail@hotmail.com
Manager
Secretary
i do need just myemail@hotmail.com
这是我的 HTML table:
</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>
尝试在 class 的 td 内获取内容。
console.log($(this).find('td.p2').text());
考虑到您的 HTML 最简单的方法是:
$('td.p2 a[href^=mailto]', html).each(function() {
console.log($(this).text().trim());
});
注意抓取后需要关闭浏览器:
let _browser;
puppeteer
.launch()
.then(function(browser) {
_browser = browser; // <-- memorize browser reference
return _browser.newPage();
})
.then(function(page) {
return page.goto(url).then(function() {
return page.content();
});
})
.then(function(html) {
$('td.p2 a[href^=mailto]', html).each(function() {
console.log($(this).text().trim());
});
})
.then(function(){
_browser.close() // <-- use it to close the browser
})
如果您是 运行 节点 8+,最好对此类脚本使用 async/await。
我正在尝试使用 cheerio 和 puppeteer 模块从我的 HTML 回复中提取电子邮件 (myemail@hotmail.com)。但我得到了不同的东西,我根本不需要使用它们。 它位于 td/tr 中的 Class 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
});
我得到这个输出:
Mobile Post box Circuit
myemail@hotmail.com
E-mail myemail@hotmail.com Manager Secretaryi do need just myemail@hotmail.com
这是我的 HTML table:
</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>
尝试在 class 的 td 内获取内容。
console.log($(this).find('td.p2').text());
考虑到您的 HTML 最简单的方法是:
$('td.p2 a[href^=mailto]', html).each(function() {
console.log($(this).text().trim());
});
注意抓取后需要关闭浏览器:
let _browser;
puppeteer
.launch()
.then(function(browser) {
_browser = browser; // <-- memorize browser reference
return _browser.newPage();
})
.then(function(page) {
return page.goto(url).then(function() {
return page.content();
});
})
.then(function(html) {
$('td.p2 a[href^=mailto]', html).each(function() {
console.log($(this).text().trim());
});
})
.then(function(){
_browser.close() // <-- use it to close the browser
})
如果您是 运行 节点 8+,最好对此类脚本使用 async/await。