如何使用Simple HTML Dom解析网站页面

时间:2016-12-03 14:53:21

标签: php html dom

我使用Simple HTML Dom创建了一个PHP代码来解析一些网页,但我很难得到一个值。

这是我的HTML代码:

    <div class="box-overflow"><div class="box-overflow__in"><table class="table-main js-tablebanner-t js-tablebanner-ntb"><tr><th class="h-text-left" colspan="2">13. Round</th><th class="h-text-center">1</th><th class="h-text-center">X</th><th class="h-text-center">2</th><th>&nbsp;</th></tr>
<tr><td class="h-text-left"><a href="/soccer/england/premier-league/arsenal-bournemouth/CEt1YwN5/" class="in-match"><span>Arsenal</span> - <span>Bournemouth</span></a></td><td class="h-text-center"><a href="/soccer/england/premier-league/arsenal-bournemouth/CEt1YwN5/">3:1</a></td><td class="table-matches__odds colored"><span><span><span data-odd="1.34"></span></span></span></td><td class="table-matches__odds" data-odd="5.35"></td><td class="table-matches__odds" data-odd="9.05"></td><td class="h-text-right h-text-no-wrap">27.11.2016</td></tr>
<tr><td class="h-text-left"><a href="/soccer/england/premier-league/manchester-united-west-ham/WYRHY7Yo/" class="in-match"><span>Manchester United</span> - <span>West Ham</span></a></td><td class="h-text-center"><a href="/soccer/england/premier-league/manchester-united-west-ham/WYRHY7Yo/">1:1</a></td><td class="table-matches__odds" data-odd="1.35"></td><td class="table-matches__odds colored"><span><span><span data-odd="5.32"></span></span></span></td><td class="table-matches__odds" data-odd="8.93"></td><td class="h-text-right h-text-no-wrap">27.11.2016</td></tr>
<tr><td class="h-text-left"><a href="/soccer/england/premier-league/southampton-everton/QeRLXRmh/" class="in-match"><span>Southampton</span> - <span>Everton</span></a></td><td class="h-text-center"><a href="/soccer/england/premier-league/southampton-everton/QeRLXRmh/">1:0</a></td><td class="table-matches__odds colored"><span><span><span data-odd="2.11"></span></span></span></td><td class="table-matches__odds" data-odd="3.37"></td><td class="table-matches__odds" data-odd="3.70"></td><td class="h-text-right h-text-no-wrap">27.11.2016</td></tr>
<tr><td class="h-text-left"><a href="/soccer/england/premier-league/watford-stoke-city/vaVPWo3b/" class="in-match"><span>Watford</span> - <span>Stoke City</span></a></td><td class="h-text-center"><a href="/soccer/england/premier-league/watford-stoke-city/vaVPWo3b/">0:1</a></td><td class="table-matches__odds" data-odd="2.27"></td><td class="table-matches__odds" data-odd="3.28"></td><td class="table-matches__odds colored"><span><span><span data-odd="3.41"></span></span></span></td><td class="h-text-right h-text-no-wrap">27.11.2016</td></tr>

这是我的PHP代码(使用Simple HTML Dom):

    <?php
include('../simple_html_dom.php');

$html = file_get_html('http://www.betexplorer.com/soccer/england/premier-league/results/');

$match_dates = $html->find("td[class=h-text-right h-text-no-wrap]"); // we have 1 per match
$titles = $html->find("td[class=h-text-left]"); // 1 per match
$results = $html->find("td[class=h-text-center]"); // 1
$odds = $html->find("td[class=table-matches__odds]"); // 3


$c=0; $b=0; 
foreach ($titles as $match) {
    list($num1, $num2) = explode(':', $results[$c]->innertext); 
    echo "<tr><td class='rtitle'>".
    "<td class='last-cell'>".$match_dates[$c]->innertext . "</td> " .
         "<td class='first-cell tl'>".$match->innertext."</td> "  .   
          "<td class='results'>".$results[$c++]->innertext."</td> "  .        
         "<td class='odds'>".$odds[$b++]->attr['data-odd'] . ";" .
         "".$odds[$b++]->attr['data-odd'] . ";" .
         "".$odds[$b++]->attr['data-odd'] . "</td>" .

         "</td></tr><br/>";
}

?>

这是我的输出:

enter image description here

事实上,我必须得到三个赔率,但我错过了这个HTML标签的价值:

<td class="table-matches__odds colored"><span><span><span data-odd="1.34"></span></span></span></td>

如果您需要更多信息来帮助我找到解决方案,请问我。谢谢!

0 个答案:

没有答案