简体   繁体   English

尝试使用 jQuery 从表中提取数据

[英]Trying to pull data from a table using jQuery

Just to give a background of the question, I am trying to pull data from an html website which was made using tables.只是为了给出问题的背景,我试图从使用表格制作的 html 网站中提取数据。 I have managed to pull most of them but there's just one thing which is troubling my brains.我设法把它们中的大部分都拉了出来,但只有一件事让我的大脑感到不安。 Maybe I need a break from work?也许我需要休息一下?

I have included all the code in a fiddle which can be found here.我已经将所有代码都包含在一个小提琴中,可以在这里找到。 https://jsfiddle.net/ex1j6gr4/ https://jsfiddle.net/ex1j6gr4/

Basically I am trying to pull the article date and author from that particular.基本上,我试图从该特定内容中提取文章日期和作者。 So I am looping through the in that and getting the element which has the date and the author using certain keywords.所以我在其中循环并使用某些关键字获取具有日期和作者的元素。 Using font:nth-child is not possible because not all the count of tag is not the same in every page.使用 font:nth-child 是不可能的,因为并非所有标签的计数在每个页面中都不相同。 (You can see two empty ones in the jsfiddle table which was a mistake) (您可以在 jsfiddle 表中看到两个空的,这是一个错误)

For the date, I have made an array of the month names and its easy to pull through that.对于日期,我制作了一系列月份名称,并且很容易通过它。

For the author, I am detecting the first word of that element's text which is "By" and its doing its job as well.对于作者,我正在检测该元素文本的第一个单词“By”,并且它也在发挥作用。

However the problem I am facing is when I am using that element outside the ".each" function which is returning the value as "undefined".然而,我面临的问题是,当我在“.each”function 之外使用该元素时,该元素将返回值为“未定义”。 Here's the jQuery code I am using.这是我正在使用的 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);

Any help would be greatly appreciated !任何帮助将不胜感激 !

You needed to define your variables outside of your functions (you had 2 loops and the second was trying to reference variables defined outside of it's scope).您需要在函数之外定义变量(您有 2 个循环,第二个是试图引用在其范围之外定义的变量)。 Here I've combined the 2 loops, removed many of the var - you only need to define that once and then you can reference the actual variable after that.在这里,我合并了 2 个循环,删除了许多var - 您只需要定义一次,然后您就可以引用实际变量。

Finally, jQuery couldn't find ('td') unless it was actually sitting inside a <table> tag.最后,jQuery 无法找到('td') ,除非它实际上位于<table>标记内。 I didn't have a function you were referencing so I put in a little forEach loop to test for the month.我没有你引用的 function 所以我放了一个小 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM