douan6931 2014-01-20 00:38
浏览 56
已采纳

PHP - DOM Parser不会返回字符串

OK, I have an HTML DOM Parser. It works... sorta. It's trying to get the text from a div witch has a class.

See the main file

<!DOCTYPE HTML>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html" />

  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
  </script>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js">
  </script>
  <style>
    #txt_out {
        border: 2px solid #C2C2C2;
        color: #2E2E2E;
        background: #EDEDED;
        width: 97%;
        padding: 5px;
        font-size: 12px;
        font-family: monospace;
        outline: none;
        height: 800px;
        margin: 10px 0;
      }
  </style>
  <title>Get Ranks</title>

  <script type="text/javascript" >   
    $(document).on("click", ".go", function (event) {

        var ID = $(".rank").val();
        console.log("Loading rank: " + ID);
        loadData(ID);


    });


    function loadData(ID) {
        var getRank = ID;

        var dataString = 'getRank=' + getRank;

        $.ajax({
            type: "POST",
            url: "otherTest.php",
            data: dataString,
            cache: false,
            success: function (html) {

                $("#txt_out").append(html);

            }
        });
    }
    </script>
  </head>
  <body>
    <h1>Get Mens Rankings</h1>

    <input value="233" class="rank" />

    <button class="go">Get stats</button>

    <textarea readonly="readonly" id="txt_out"></textarea>

  </body>
</html>

and upon clicking the 'get stats' button, it calls (through AJAX) this page:

<?php
include_once ('simple_html_dom.php');

$rank = $_POST['getRank'];

$URL = "http://fifa.com/worldranking/rankingtable/gender=m/rank=".$rank."/confederation=25998/page=1/_ranking_table.html";

$html = file_get_html($URL);

$test = trim($html->find('.rnkdate', 0)->innertext);
echo "Date published: " . $test;

?>

It's trying to extract the date published from this URL: http://www.fifa.com/worldranking/rankingtable/gender=m/rank=233/confederation=25998/page=1/_ranking_table.html where rank=XXX is a different table for a different month.

Anyway, when I do this, here's what I get:

enter image description here

The problem is that it's inside another DIV and thus... I guess it doesn't show inside a <textarea>. So... how do I get inside that div and extract the text itself? Thank you.

  • 写回答

1条回答 默认 最新

  • douwen3965 2014-01-20 00:48
    关注

    if you're using the http://simplehtmldom.sourceforge.net/ , then according to http://simplehtmldom.sourceforge.net/manual_api.htm , seems you should be using "plaintext", not "innertext" (as innertext seems to be what javascript devs call innerHTML , while "plaintext" seems to be the js equivalent of .textContent ... this simplehtmldom lib seems to be written by people who aren't web devs... whatever) anyway

    $test = trim($html->find('.rnkdate', 0)->plaintext);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败