Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/367.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript获取HTML表内容?_Javascript_Greasemonkey_Tampermonkey - Fatal编程技术网

如何使用javascript获取HTML表内容?

如何使用javascript获取HTML表内容?,javascript,greasemonkey,tampermonkey,Javascript,Greasemonkey,Tampermonkey,我正在创建一个Greasemonkey脚本,它从基于文本的游戏中读取信息并将其存储到数据库中,以便将来使用 我想要的是能够读取用户的统计数据,并将这些统计数据转换为变量,这样我就可以继续使用这些信息 下面是我想从中获取统计信息的表的源代码: <table width="100%"> <tr> <td width="50%" valign="top" style="padding-right: 25px;"> <table class="tabl

我正在创建一个Greasemonkey脚本,它从基于文本的游戏中读取信息并将其存储到数据库中,以便将来使用

我想要的是能够读取用户的统计数据,并将这些统计数据转换为变量,这样我就可以继续使用这些信息

下面是我想从中获取统计信息的表的源代码:

<table width="100%">
<tr>
<td width="50%" valign="top" style="padding-right: 25px;">
    <table  class="table_lines" width="100%" cellspacing="0" cellpadding="6" border="0">
    <tr>
        <th colspan="3">Military Effectiveness</th>
    </tr>
    <tr>
        <td><b>Strike Action</b></td>
        <td align="right">16,376,469,657</td>
        <td align="right">Ranked #443</td>
    </tr>
    <tr>
        <td><b>Defensive Action</b></td>
        <td align="right">4,016,716,436</td>
        <td align="right">Ranked #569</td>
    </tr>
    <tr>
        <td><b>Spy Rating</b></td>
        <td align="right">12,245,896</td>
        <td align="right">Ranked #1,204</td>
    </tr>
    <tr>
        <td><b>Sentry Rating</b></td>
        <td align="right">5,291,630,090</td>
        <td align="right">Ranked #831</td>
    </tr>
</table>

军事效能
罢工行动
16,376,469,657
排名#443
防御行动
4,016,716,436
排名#569
间谍等级
12,245,896
排名#1204
哨兵等级
5,291,630,090
排名#831
现在您可以看到,统计数据没有识别类ID或任何东西,所以我不知道如何做到这一点。我只真正使用PHP,所以JS对我来说很新,但它似乎与PHP相似

也许是说“在罢工行动之后,获取第一个td值”,然后将其作为变量


注意:攻击行动、防御行动、间谍等级和哨兵等级是我需要的变量。

您将为其分配一个id,并使用getElementById javascript方法获取它:

HTML

但是,如果您试图从非自己的页面获取内容,xpath是一种方法:

function getElement(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

var rows = getElement("//html[1]/body[1]/table[@class='table_lines']/tr");
这将返回所有表行的数组

list.getElementsByTagName("tag").innerHTML = "html text";
这可能也可以工作

尝试类似的方法(注意,您需要使用jquery库才能工作)

  • 用于简化表的解析
  • 因为您需要评级,所以不要忘记将数字解析为javascript整数
  • 如果页面是AJAX驱动的,请使用
  • 下面是一个完整的Greasemonkey/Tampermonkey脚本,展示了如何完成所有这些:

    // ==UserScript==
    // @name     _Parse table information that has low information scent.
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @include  http://bilalrammal.ca/clicker/tester.html
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    waitForKeyElements (".table_lines", parseMilitaryEffectivenessTable);
    
    function parseMilitaryEffectivenessTable (jNode) {
        //--- Note that :contains() is case-sensitive.
        var strikeAction    = jNode.find ("tr:contains('Strike Action') td:eq(1)").text ();
        var defensiveAction = jNode.find ("tr:contains('Defensive Action') td:eq(1)").text ();
        var spyRating       = jNode.find ("tr:contains('Spy Rating') td:eq(1)").text ();
        var sentryRating    = jNode.find ("tr:contains('Sentry Rating') td:eq(1)").text ();
    
        //--- Convert strings to integers...
        strikeAction        = parseInt (strikeAction   .replace (/\D/g, ""), 10);
        defensiveAction     = parseInt (defensiveAction.replace (/\D/g, ""), 10);
        spyRating           = parseInt (spyRating      .replace (/\D/g, ""), 10);
        sentryRating        = parseInt (sentryRating   .replace (/\D/g, ""), 10);
    
        //--- Show on console:
        console.log ("strikeAction: ",       strikeAction);
        console.log ("defensiveAction: ",    defensiveAction);
        console.log ("spyRating: ",          spyRating);
        console.log ("sentryRating: ",       sentryRating);
    }
    

    没有ID,这就是我要说的:我自己不能给它分配ID,因为它不是我自己的页面,是GreaseMonkey用来读取信息的页面。啊,我误读了。您可以研究xpath。那会把工作完成的。是的,那实际上可能会起作用——但我不知道如何实施它。。也许是第一个变量的一个例子,我可以知道如何使用它-我的javascript非常糟糕。这个答案和另一个答案似乎很有道理,但当我测试它时,它们不会为我返回任何东西。。JSFIDLE上的一个工作示例现在会有很大帮助:/I我会修改它以保存一个统计数据数组。这是一把小提琴:@Brock Adams,很好。我同意。更简单。我只会将两件事更改为“keyValues”和“stats”,并且我会将“var”声明链接起来。这与OP所需要的并不接近。他想看一张表。最后一件事,我该怎么做才能让它只精确地指出td的打击行动价值,谁的父母是th,谁的价值是“军事效力”?由于如果有多个表具有类
    表\u行
    ,并且它们都不是“军事有效性”,则页面上可能会显示罢工行动,因此请调整
    WaitForkEyements()中使用的选择器。
    。类似于:
    ”。表行:包含('军力')“
    是一种方式。如果是别的问题,提出一个新问题。
    list.getElementsByTagName("tag").innerHTML = "html text";
    
    $(".table_lines").find('tr').each(function (i) {
        var $tds = $(this).find('td'),
            lable = $tds.eq(0).text(),
            value = $tds.eq(1).text(),
            rank = $tds.eq(2).text();
        // do something with lable, value, rank
        alert('Lable: ' + lable + '\nValue: ' + value + '\nRank: ' + rank);
    })
    
    // ==UserScript==
    // @name     _Parse table information that has low information scent.
    // @include  http://YOUR_SERVER.COM/YOUR_PATH/*
    // @include  http://bilalrammal.ca/clicker/tester.html
    // @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    GM_addStyle
    // ==/UserScript==
    /*- The @grant directive is needed to work around a design change
        introduced in GM 1.0.   It restores the sandbox.
    */
    waitForKeyElements (".table_lines", parseMilitaryEffectivenessTable);
    
    function parseMilitaryEffectivenessTable (jNode) {
        //--- Note that :contains() is case-sensitive.
        var strikeAction    = jNode.find ("tr:contains('Strike Action') td:eq(1)").text ();
        var defensiveAction = jNode.find ("tr:contains('Defensive Action') td:eq(1)").text ();
        var spyRating       = jNode.find ("tr:contains('Spy Rating') td:eq(1)").text ();
        var sentryRating    = jNode.find ("tr:contains('Sentry Rating') td:eq(1)").text ();
    
        //--- Convert strings to integers...
        strikeAction        = parseInt (strikeAction   .replace (/\D/g, ""), 10);
        defensiveAction     = parseInt (defensiveAction.replace (/\D/g, ""), 10);
        spyRating           = parseInt (spyRating      .replace (/\D/g, ""), 10);
        sentryRating        = parseInt (sentryRating   .replace (/\D/g, ""), 10);
    
        //--- Show on console:
        console.log ("strikeAction: ",       strikeAction);
        console.log ("defensiveAction: ",    defensiveAction);
        console.log ("spyRating: ",          spyRating);
        console.log ("sentryRating: ",       sentryRating);
    }