Foreach并跳过特定的HTML

时间:2017-03-18 10:27:42

标签: php html web-scraping

我正在使用foreach来详细说明从网站上删除的一些值。它有效,但我有以下问题(我用屏幕以最好的方式解释我的问题)。

在第一个屏幕中,您可以看到Derry City - Limerick没有1 X 2赔率

enter image description here

但是当我得到这些值时,我的代码会以可用的第一个赔率填补空赔率,请参阅下面的屏幕:

enter image description here

这是HTML代码:

    <table class="table-main js-tablebanner-t js-tablebanner-ntb">
<tr><th class="h-text-left" colspan="2">2. Round</th><th>&nbsp;</th><th class="table-main__bs">B's</th><th class="h-text-center">1</th><th class="h-text-center">X</th><th class="h-text-center">2</th></tr>
<tr>
<td class="table-main__datetime">21.03.2017 20:45</td>
<td class="h-text-left"><a href="/soccer/ireland/premier-division/derry-city-limerick/UksvnJBq/" class="in-match"><span>Derry City</span> - <span>Limerick</span></a></td>
<td class="h-text-right">
<ul class="list-tags"><li class="list-tags__item"><i class="icon icon__stream"></i><div class="list-tags__window list-tags__window--streams"><ul class="list-tags__window__in">
<li>Live streams</li>
<li><a href="/bookmaker/261/https://mediaserver.bwinpartypartners.com/renderBanner.do?zoneId=1641737" target="_blank">bwin</a></li>
<li><a href="/bookmaker/27/http://partner.sbaffiliates.com/processing/clickthrgh.asp?btag=a_38477b_991" target="_blank">Sportingbet</a></li>
</ul></div></li></ul>
</td>
<td class="table-main__bs">&nbsp;</td>
<td class="table-matches__odds" data-oid="">&nbsp;</td>
<td class="table-matches__odds" data-oid="">&nbsp;</td>
<td class="table-matches__odds" data-oid="">&nbsp;</td>
</tr>
<tr><th class="h-text-left" colspan="2">5. Round</th><th>&nbsp;</th><th class="table-main__bs">B's</th><th class="h-text-center">1</th><th class="h-text-center">X</th><th class="h-text-center">2</th></tr>
<tr>
<td class="table-main__datetime">18.03.2017 15:00</td>
<td class="h-text-left"><a href="/soccer/ireland/premier-division/limerick-finn-harps/nakgESyg/" class="in-match"><span>Limerick</span> - <span>Finn Harps</span></a></td>
<td class="table-matches__odds" data-oid="2kou5xv464x0x5uaff"><a href="/myselections.php?action=3&matchid=nakgESyg&outcomeid=2kou5xv464x0x5uaff&otheroutcomes=2kou5xv498x0x0,2kou5xv464x0x5uafg" onclick="return my_selections_click('1x2', 'soccer');" title="Add to My Selections" target="mySelections" data-odd="1.40"></a></td>
<td class="table-matches__odds" data-oid="2kou5xv498x0x0"><a href="/myselections.php?action=3&matchid=nakgESyg&outcomeid=2kou5xv498x0x0&otheroutcomes=2kou5xv464x0x5uaff,2kou5xv464x0x5uafg" onclick="return my_selections_click('1x2', 'soccer');" title="Add to My Selections" target="mySelections" data-odd="4.42"></a></td>
<td class="table-matches__odds" data-oid="2kou5xv464x0x5uafg"><a href="/myselections.php?action=3&matchid=nakgESyg&outcomeid=2kou5xv464x0x5uafg&otheroutcomes=2kou5xv464x0x5uaff,2kou5xv498x0x0" onclick="return my_selections_click('1x2', 'soccer');" title="Add to My Selections" target="mySelections" data-odd="6.91"></a></td>
</tr>

<tr>
<td class="table-main__datetime">18.03.2017 16:00</td>
<td class="h-text-left"><a href="/soccer/ireland/premier-division/derry-city-drogheda/G6qjqHe2/" class="in-match"><span>Derry City</span> - <span>Drogheda</span></a></td>
<td class="table-main__bs">23</td>
<td class="table-matches__odds" data-oid="2kp2nxv464x0x5uaoj"><a href="/myselections.php?action=3&matchid=G6qjqHe2&outcomeid=2kp2nxv464x0x5uaoj&otheroutcomes=2kp2nxv498x0x0,2kp2nxv464x0x5uaok" onclick="return my_selections_click('1x2', 'soccer');" title="Add to My Selections" target="mySelections" data-odd="1.44"></a></td>
<td class="table-matches__odds" data-oid="2kp2nxv498x0x0"><a href="/myselections.php?action=3&matchid=G6qjqHe2&outcomeid=2kp2nxv498x0x0&otheroutcomes=2kp2nxv464x0x5uaoj,2kp2nxv464x0x5uaok" onclick="return my_selections_click('1x2', 'soccer');" title="Add to My Selections" target="mySelections" data-odd="4.09"></a></td>
<td class="table-matches__odds" data-oid="2kp2nxv464x0x5uaok"><a href="/myselections.php?action=3&matchid=G6qjqHe2&outcomeid=2kp2nxv464x0x5uaok&otheroutcomes=2kp2nxv464x0x5uaoj,2kp2nxv498x0x0" onclick="return my_selections_click('1x2', 'soccer');" title="Add to My Selections" target="mySelections" data-odd="7.08"></a></td>
</tr>

,至少,这是我的代码:

    $match_dates = $html->find("td[class=table-main__datetime]"); 
$titles = $html->find("a[class=in-match]"); // 1 per match
$odds = $html->find("td[class=table-matches__odds]/a"); // 2

function print_odd($odd) {
    if (array_key_exists('data-odd', $odd->attr)) {
        return $odd->attr['data-odd'];
    }

    return $odd->children(0)->children(0)->children(0)->attr['data-odd'];
}


$c=0; $b=0; $o=0; $z=0; // two counters
foreach ($titles as $match) {


    list($num1, $num2) = explode(':', $res->innertext); // <- explode   


    list($home, $away) = explode('-', $titles[$z++]->innertext); // <- explode
    list($date, $time) = explode('-', $match_dates[$o++]->innertext); // <- explode


$odd1 = print_odd($odds[$b++]);
$odd2 = print_odd($odds[$b++]);
$odd3 = print_odd($odds[$b++]);     


$home = strip_tags($home);
$away = strip_tags($away);
$uniquefield = $home . ' ' . $away;

            echo "<tr><td class='rtitle'>".
    //"<td class='last-cell'>".$match_dates[$o++]->innertext. "</td> " .
                "<td> ".$date.'</td><td> : </td><td>'.$time .  " / " .  

            "<td> ".$home.'</td><td> : </td><td>'.$away .  " / " .  

            "<td class='odds'>".$odd1 . ";" .
            "".$odd2  . ";" .
            "".$odd3  . "</td>" .
            "".$uniquefield."</td></tr><br/>";

0 个答案:

没有答案