PHP解析将找不到“span”标记

时间:2015-05-31 02:27:57

标签: php html parsing xpath domdocument

我正在尝试在类似这样的网站上找到span标记:http://www.pointstreak.com/prostats/leagueschedule.html?leagueid=49&seasonid=14225。我需要的标签是:

enter image description here

但是,当我使用如下代码时:

    $my_url = 'http://www.pointstreak.com/prostats/leagueschedule.html?leagueid=49&seasonid=14225';
    $html = file_get_contents($my_url);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom);

    //Put your XPath Query here
    $my_xpath_query = "//span";
    $result_rows = $xpath->query($my_xpath_query);

    // Create an array to hold the content of the nodes
    $statsListings = array();

    //here we loop through our results (a DOMDocument Object)
    foreach ($result_rows as $result_object) {
        $statsListings[] = $result_object->nodeValue;
    }


    echo json_encode($statsListings);

我得到的唯一输出是[]

如果我将$statsListings[] = $result_object->nodeValue;替换为$statsListings[] = $result_object->childNodes->item(0)->nodeValue;,我仍会获得与输出相同的[]。如果有明确的span标记值,为什么我什么都没得到?

1 个答案:

答案 0 :(得分:3)

XPath完全无罪。 跨度标签以dinamically方式添加。只看一下页面的源代码,而不是DOM-Structure,它可能已被javascript修改过,但使用“view-source:”,你会看到完全相同的html,因为它是由XPath解析的。

查看带有表格类的表格会是一个好主意吗?可能,你有可能需要的一切。 你应该跳过“maincolor”和“tableheader”,然后用“light”类开始处理。

<table width="98%" class="tablelines" cellpadding="2" border="0" cellspacing="1">
<tr class="maincolor">
    <td colspan="8" align="right">All Times Local</td>
</tr>
<tr class="tableheader">
    <td width="4%">
        <b>GN</b>
    </td>
    <td nowrap width="21%">
        <b>AWAY</b>
    </td>
    <td nowrap width="21%">
        <b>HOME</b>
    </td>
    <td width="14%"><b>DATE</b></td>
    <td width="11%"><b>TIME</b></td>
    <td width="8%"><b>SCORE</b></td>
    <td nowrap align="right" width="*"><b>BOXSCORE</b></td>
    <td nowrap align="center" width="4%"><b>GS</b></td>
</tr>
<tr class="light">
    <td></td>
    <td><a href="teamplayerstats.html?teamid=3138&seasonid=14225">Sioux City</a>
        <b>1</b></td>
    <td><a href="teamplayerstats.html?teamid=3139&seasonid=14225">Sioux Falls</a>
        <b>5</b></td>
    <td>Tue, Apr 14</td>
    <td> 7:05 PM</td>
    <td> <b>1 - 5</b> </td>
    <td align="right">
        <a href="http://www.pointstreak.com/flashapp/index_hockey_new.html?gameid=2657671" target="_blank"><img src="/images/gamelive_icon.gif" title="Click here for Game Live!" alt="Click here for Game Live" border="0"></a>
        <a href="boxscore.html?gameid=2657671">Final</a></td>
    <td align="center">
        <a href="gamesheet_full.html?gameid=2657671" target="_blank"><img src="/images/playersection/prostats/gslink.gif" border="0"></a>
    </td>
</tr>

例如,试试这个:

$my_url = 'http://www.pointstreak.com/prostats/leagueschedule.html?leagueid=49&seasonid=14225';
$html = file_get_contents($my_url);
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

//Put your XPath Query here
$my_xpath_query = "//tr[@class='light']/td";
$result_rows = $xpath->query($my_xpath_query);
echo $result_rows->length;
// Create an array to hold the content of the nodes
$statsListings = array();

//here we loop through our results (a DOMDocument Object)
foreach ($result_rows as $result_object) {
    $statsListings[] = $result_object->nodeValue;
}

echo json_encode($statsListings);

可能我找到了你需要的东西,甚至是以漂亮的JSON形式: http://www.pointstreak.com/ajax/trending_ajax.html?action=divisionscoreboard&divisionid=12299&seasonid=14225

{"trending_list":null,"lacrosse_list":null,"hockey_list":null,"soccer_list":null,"baseball_list":null,"softball_list":null,"basketball_list":null,"news_list":null,"news_hockey_list":null,"news_baseball_list":null,"news_baseball_list2":null,"news_softball_list":null,"news_basketball_list":null,"games_list":[{"status":"FINAL","hometeam":"Sioux Falls","homescore":"4","awayteam":"Muskegon","awayscore":"2","timeremaining":"0:00","currentperiod":"3rd","schedtime":"7:05 pm","gamedate":"15\/05","link":"..\/prostats\/boxscore.html?gameid=2672134"},{"status":"FINAL","hometeam":"Muskegon","homescore":"1","awayteam":"Sioux Falls","awayscore":"6","timeremaining":"0:00","currentperiod":"3rd","schedtime":"7:15 pm","gamedate":"10\/05","link":"..\/prostats\/boxscore.html?gameid=2672133"},{"status":"FINAL","hometeam":"Muskegon","homescore":"2","awayteam":"Sioux Falls","awayscore":"3","timeremaining":"0:00","currentperiod":"1st","schedtime":"7:15 pm","gamedate":"09\/05","link":"..\/prostats\/boxscore.html?gameid=2672132"},{"status":"FINAL","hometeam":"Dubuque","homescore":"3","awayteam":"Muskegon","awayscore":"4","timeremaining":"0:00","currentperiod":"3rd","schedtime":"7:05 pm","gamedate":"05\/05","link":"..\/prostats\/boxscore.html?gameid=2662061"},{"status":"FINAL","hometeam":"Muskegon","homescore":"0","awayteam":"Dubuque","awayscore":"6","timeremaining":"0:00","currentperiod":"3rd","schedtime":"7:15 pm","gamedate":"02\/05","link":"..\/prostats\/boxscore.html?gameid=2662060"},{"status":"FINAL","hometeam":"Sioux Falls","homescore":"7","awayteam":"Tri-City","awayscore":"3","timeremaining":"0:00","currentperiod":"3rd","schedtime":"7:05 pm","gamedate":"02\/05","link":"..\/prostats\/boxscore.html?gameid=2662055"},{"status":"FINAL","hometeam":"Muskegon","homescore":"3","awayteam":"Dubuque","awayscore":"1","timeremaining":"0:00","currentperiod":"3rd","schedtime":"7:15 pm","gamedate":"01\/05","link":"..\/prostats\/boxscore.html?gameid=2662059"},{"status":"FINAL","hometeam":"Sioux Falls","homescore":"4","awayteam":"Tri-City","awayscore":"3","timeremaining":"0:00","currentperiod":"3rd","schedtime":"7:04 pm","gamedate":"01\/05","link":"..\/prostats\/boxscore.html?gameid=2662054"},{"status":"FINAL","hometeam":"Tri-City","homescore":"2","awayteam":"Sioux Falls","awayscore":"3","timeremaining":"0:00","currentperiod":"3rd","schedtime":"7:05 pm","gamedate":"29\/04","link":"..\/prostats\/boxscore.html?gameid=2664638"},{"status":"FINAL","hometeam":"Dubuque","homescore":"7","awayteam":"Muskegon","awayscore":"3","timeremaining":"0:00","currentperiod":"3rd","schedtime":"7:05 pm","gamedate":"25\/04","link":"..\/prostats\/boxscore.html?gameid=2662058"}],"division_list":null,"site_network_title":null,"leagueshortname":"USHL","includesportlink":null,"showleaguename":0}