PHP解析找不到 "span"标签

标签 php html parsing xpath domdocument

我正在尝试在类似于此的网站上查找跨度标签: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; code>,我仍然得到相同的 [] 作为输出。当明显有带有值的 span 标签时,为什么我什么也得不到?

最佳答案

XPath 根本没有罪。 Span 标签是动态添加的。只需查看页面的源代码,而不是 DOM-Structure,它可能已被 javascript 修改,但使用“view-source:”,您将看到完全相同的 html,因为它是由 XPath 解析的。

看看带有tablelines类的表格是个好主意吗?也许,那里有您可能需要的一切。 您应该跳过“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}

关于PHP解析找不到 "span"标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30553241/

相关文章:

php - FosRestBundle PATCH 操作阻止具有空值/默认值的更新实体

perl - 是否有用于解析柱状文本的 Perl 模块?

c - 我如何在 AWK 中解析 C switch-case 和 for Statement?

php - PHP解析/语法错误;以及如何解决它们

php - json_encode 给出递归错误

javascript - jquery Accordion 但从另一个脚本加载内容

php - 将 javascript 值分配给 php 变量

html - 定位 li 元素以覆盖其父 ul 边框以制作制表符?

html - IE 将 "top"设置为百分比值不适用于表格单元格内的绝对定位元素

javascript - getElementByID 输出到这里的是什么对象?