简单的HTML Dom更改链接源dinamically

时间:2017-10-20 09:10:25

标签: php html simple-html-dom

我有一个页面,我提取" a href"链接到特定的" td类"。这是我的代码和输出:

extractlink.php

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


function getHTML($url,$timeout)
{
       $ch = curl_init($url); // initialize curl with given url
       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set  useragent
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
       curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       return @curl_exec($ch);
}

$linkurl = "http://www.betexplorer.com/soccer/czech-republic/1-liga/results/";
$response=getHTML($linkurl);

$html = str_get_html($response);


foreach($html->find("td[class=h-text-center]/a") as $div) {


    $show = 'http://www.betexplorer.com' .$div->href . '<br>';

    echo $show;
}

?>

extractlink.php输出:

enter image description here

现在我想将每个链接动态地放入我的getHTML网址到我的single.php页面。您可以在下面看到代码和输出:

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

function getHTML($url,$timeout)
{
       $ch = curl_init($url); // initialize curl with given url
       curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set  useragent
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
       curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
       curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
       curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       return @curl_exec($ch);
}

$response=getHTML("http://www.betexplorer.com/soccer/czech-republic/1-liga/brno-slovacko/Sxc6EbMS/",10);
$html = str_get_html($response);


$titles = $html->find("span[class=list-breadcrumb__item__in]"); // 1 per match
$result = $html->find("p[id=js-score]"); // 1
$partial = $html->find("h2[id=js-partial]"); // 1
$best_bets = $html->find("td[class=table-matches__odds colored]/span/span/span"); // 1
$odds = $html->find("td[class=table-matches__odds]"); // 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; $h=0; $d=0; $s=0;// two counters
foreach ($titles as $match) {

  list($num1, $num2) = explode(':', $result[$c++]->innertext); // <- explode
    $num1 = intval($num1);
    $num2 = intval($num2);
    $num3 = ($num1 + $num2);
    $risultato = ($num1 . '-' . $num2);
    $risultatounito = ($num1 . '-' . $num2);

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

    list($partialht, $partialft) = explode(',', $partial[$o++]->innertext); // <- explode

$partialht = str_replace('(', '', $partialht);
$partialft = str_replace(')', '', $partialft);


$rest = substr($partialht, -1);
$firstCharacter = $partialht[0];
$lastCharacter = $partialht[2];
$firstCharacters = $partialft[1];
$lastCharacters = $partialft[3];

    $firstCharacter = intval($firstCharacter);
    $lastCharacter = intval($lastCharacter);

    $firstCharacters = intval($firstCharacters);
    $lastCharacters = intval($lastCharacters);

    $somma = $firstCharacter + $lastCharacter;
    $sommatwo = $firstCharacters + $lastCharacters;

    list($homescoreht, $awayscoreht) = explode(':', $partialht[$d++]->innertext); // <- explode

    $homescoreht = intval($homescoreht);
    $awayscoreht = intval($awayscoreht);

    list($homescoreft, $awayscoreft) = explode(':', $partialft[$s++]->innertext); // <- explode

    $homescoreft = intval($homescoreft);
    $awayscoreft = intval($awayscoreft);


    if ($somma > 0){

        $over05ht = "Over 0.5 HT";
    }else{

        $over05ht = "Under 0.5 HT";

    }

    if ($num3 > $somma){

        $over05sh = "Over 0.5 SH";
    }else{

        $over05sh = "Under 0.5 SH";

    }


$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> ".$home.'</td><td> : </td><td>'.$away .  " / " .  // <- example use
            "<td> ".$num1.'</td><td> : </td><td>'.$num2 .  " / " .  // <- example use
            "<td class='first-cell'>".$partialht ."</td> "  .
            "<td class='first-cell'>".$partialft ."</td> "  .
            "<td class='first-cell'>".$firstCharacter ."</td> "  .
            "<td class='first-cell'>".$lastCharacter ."/</td> "  .
            "<td class='first-cell'>".$firstCharacters ."</td> "  .
            "<td class='first-cell'>".$lastCharacters ."/</td> "  .
            "<td class='first-cell'>".$somma ."/</td> "  .
            "<td class='first-cell'>".$num3 ."/</td> "  .
            "<td class='first-cell'>".$over05ht ."/</td> "  .
            "<td class='first-cell'>".$over05sh ."/</td> "  .



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

            "<td class='first-cell'>".$uniquefield ."</td> "  .

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



        }   //close foreach 


?>

single.php输出:

output:

所以,我想将getHTML url更改为single.php并从extractlink.php中插入每个url

0 个答案:

没有答案