Javascript 尝试使用Nodejs从HTML响应中提取信息

Javascript 尝试使用Nodejs从HTML响应中提取信息,javascript,html,node.js,puppeteer,cheerio,Javascript,Html,Node.js,Puppeteer,Cheerio,我只是想发电子邮件(myemail@hotmail.com)从我使用cheerio和Puppeter模块的HTML响应中。但是我得到了不同的东西,我根本不需要全部使用它们。 其在td/tr中被置于p2类中。 将tr作为参数放入 我的代码是这样的: const puppeteer = require('puppeteer'); const $ = require('cheerio'); const url = 'https://mywebsite.com'; puppeteer .launc

我只是想发电子邮件(myemail@hotmail.com)从我使用cheerio和Puppeter模块的HTML响应中。但是我得到了不同的东西,我根本不需要全部使用它们。 其在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());

尝试获取该类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
  })
let\u浏览器;
木偶演员
.launch()
.then(功能(浏览器){

_browser=browser;//考虑到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
  })
let\u浏览器;
木偶演员
.launch()
.then(功能(浏览器){


_browser=browser;//可以使用正则表达式吗?它看起来是解决这个问题的一个不错的解决方案。HTML看起来不像实际的HTML,因为它没有
经理秘书
。@Keith这确实是HTML表的一部分,我们感兴趣的部分已经被删除了:)@在尝试放置tr时,响应发送给我整个HTMLI的所有tr是否可以使用RegEx?这看起来是解决此问题的一个不错的解决方案。HTML看起来不像实际的HTML,因为它没有
经理秘书
。@Keith这确实是HTML表的一部分,并且我们感兴趣的部分已被删除。.:)@在尝试放置tr时,响应将我整个HTMLT的所有tr发送给我。问题是,我有许多名为p2的不同类,因此我获得了所有p2结果。;(还有其他线程如何执行此操作吗?问题是,我有许多名为p2的不同类,因此我获得了所有p2结果。);(有没有另外一条线索来说明如何做到这一点?这看起来像是在提供了我的email@hotmail.com该过程进入无限循环。在提供电子邮件后如何结束该过程?alex@/Desktop/scd$node app.jsemail@hotmail.com//没有停止循环担心我要求太多,但我只是一个初学者。关闭浏览器后,我的电子邮件中有一个很大的空白文本。如何删除它?alex@~/Desktop/scd$node app.jsmyemail@hotmail.comalex@~/Desktop/scdRemove console.log哪个打印它?我需要将此函数与node mailer函数集成,以便获取此电子邮件(var result=$(this).text().trim())并将其传递到nodemailer的函数中,例如sendmail(结果)在尝试此操作时,我从您的方法中得到的结果是空白文本,但在尝试使用console.log时,我得到了正确的结果,为什么?请说明您是如何执行此操作的,因为这是另一个问题,您应该就此提出一个新问题。在提供我的email@hotmail.com这个过程进入一个无限循环。我如何结束这个循环提供我的电子邮件后的处理?alex@/Desktop/scd$node app.jsemail@hotmail.com//没有停止循环担心我要求太多,但我只是一个初学者。关闭浏览器后,我的电子邮件中有一个很大的空白文本。如何删除它?alex@~/Desktop/scd$node app.jsmyemail@hotmail.comalex@~/Desktop/scdRemove console.log哪个优先级nts it?我需要将此函数与node mailer函数集成,以便获取此电子邮件(var result=$(this).text().trim())并将其传递到nodemailer的函数中,例如sendmail(result)在尝试此操作时,我从您的方法得到的结果是空白文本,但在尝试使用console.log时,我得到了正确的结果,为什么?请说明您是如何执行此操作的,因为这是另一个问题,您真的应该就此提出一个新问题。