【问题标题】:Trying to extract information from HTML response using Nodejs尝试使用 Nodejs 从 HTML 响应中提取信息
【发布时间】: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


【解决方案1】:

考虑到您的 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
  })

如果您运行的是 node 8+,最好对此类脚本使用 async/await。

【讨论】:

  • 在提供了我的 email@hotmail.com 之后,这个过程似乎进入了一个无限循环。提供电子邮件后如何结束流程? alex@/Desktop/scd $ node app.js email@hotmail.com //不会停止循环
  • 对不起,我要求太多,但我只是一个初学者。关闭浏览器后,我的电子邮件中有一个很大的空白文本。如何删除它? alex@~/Desktop/scd $ node app.js myemail@hotmail.com alex@~/Desktop/scd
  • 删除打印它的console.log?
  • 我需要将此功能与 node-mailer 功能集成,以便我获取此电子邮件(var result = $(this).text().trim()) 并将其传递给 nodemailer 的功能例如 sendmail(result) 在尝试这个时我从你的方法得到的结果是空白文本,但是当我尝试使用 console.log 时我得到了正确的结果,为什么?
  • 请展示你是如何做到的,既然这是另一个问题,你真的应该就此提出一个新问题。
【解决方案2】:

尝试在该类的 td 中获取内容。

console.log($(this).find('td.p2').text());

【讨论】:

  • 问题是我有许多名为 p2 的不同类,所以我得到了所有 p2 结果。 ;(还有其他关于如何做到这一点的线索吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
  • 1970-01-01
  • 2014-08-14
  • 1970-01-01
  • 2013-05-15
相关资源
最近更新 更多