首页 > 解决方案 > 尝试使用 jQuery 从表中提取数据

问题描述

只是为了给出问题的背景,我正在尝试从使用表格制作的 html 网站中提取数据。我设法把它们中的大部分都拉了出来,但只有一件事让我的大脑感到不安。也许我需要休息一下?

我已经将所有代码都包含在一个小提琴中,可以在这里找到。https://jsfiddle.net/ex1j6gr4/

基本上我试图从那个特定的 . 所以我在其中循环并使用某些关键字获取具有日期和作者的元素。使用 font:nth-child 是不可能的,因为并非所有标签的计数在每个页面中都不相同。(您可以在 jsfiddle 表中看到两个空的,这是一个错误)

对于日期,我制作了一系列月份名称,并且很容易通过它。

对于作者,我正在检测该元素文本的第一个单词“By”,并且它也在发挥作用。

然而,我面临的问题是当我在“.each”函数之外使用该元素时,该函数将值返回为“未定义”。这是我正在使用的 jQuery 代码。

function monthNames(string, keywords) {
    return string.split(/\b/).some(Array.prototype.includes.bind(keywords));
}

var months = ["Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];

jQuery('td').find('font').each(function() {
  var curtext = jQuery(this).text();
  var has_date  = monthNames(curtext, months);
  if (has_date == true) {
    var post_date = curtext;
    jQuery('#current-date-text').html(post_date);
  }
});

jQuery('#current-outside-date').html(post_date);

jQuery('td').find('font').each(function() {
  var curtext = jQuery(this).text();
  var i = curtext.indexOf(' ');
  var first_word = curtext.substring(0, i);
  if (first_word == 'By') {
    var author = curtext;
    var author = author.substr(author.indexOf(" ") + 1);
    jQuery('#current-author-text').html(author);
  }
});

jQuery('#current-outside-author').html(author);

任何帮助将不胜感激 !

标签: javascriptjquery

解决方案


您需要在函数之外定义变量(您有 2 个循环,第二个是试图引用在其范围之外定义的变量)。在这里,我结合了 2 个循环,删除了许多循环var- 您只需要定义一次,然后您就可以引用实际变量。

最后,jQuery 无法找到('td'),除非它实际上位于<table>标签内。我没有你引用的函数,所以我放了一个小 forEach 循环来测试这个月。

jQuery(document).ready(function() {
  var months = ["Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];
  var post_date, author, curtext, has_date, first_word

  jQuery('td font').each(function() {
    curtext = jQuery(this).text();
    has_date = false
    curtext.split(" ").forEach(w => {
      if (months.includes(w)) has_date = true;
    })

    if (has_date) {
      post_date = curtext;
      jQuery('#current-date-text').html(post_date);
    }


    jQuery('#current-outside-date').html(post_date);

    curtext = jQuery(this).text();
    var i = curtext.indexOf(' ');
    first_word = curtext.substring(0, i);
    if (first_word == 'By') {
      author = curtext;
      author = author.substr(author.indexOf(" ") + 1);
      jQuery('#current-author-text').html(author);
    }
  });

  jQuery('#current-outside-author').html(author);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
  <tr>
    <td width="100%">
      <font size="4" face="Times New Roman,Georgia,Times"><b>Some text over here</b></font>
      <font size="2" face="Times New Roman,Georgia,Times"></font>
      <font size="3" face="Times New Roman,Georgia,Times"><b>Some random text here again</b></font>
      <font size="2" face="Times New Roman,Georgia,Times"></font>
      <font size="3" face="Times New Roman,Georgia,Times">July 16, 2001</font>
      <font size="3" face="Times New Roman,Georgia,Times">By Author name</font>
    </td>
  </tr>
</table>

<p id="current-date-text"></p>
<p id="current-outside-date"></p>
<p id="current-author-text"></p>
<p id="current-outside-author"></p>


推荐阅读