Advertisement
Guest User

scrapingandsavephp

a guest
Jan 28th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.98 KB | None | 0 0
  1. <?php
  2. include('../simple_html_dom.php');
  3.  
  4. function getHTML($url,$timeout)
  5. {
  6.        $ch = curl_init($url); // initialize curl with given url
  7.        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set  useragent
  8.        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
  9.        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
  10.        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
  11.        curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
  12.        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  13.        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  14.        return @curl_exec($ch);
  15. }
  16.  
  17. $response=getHTML("http://www.betexplorer.com/soccer/england/premier-league/results/",10);
  18. $html = str_get_html($response);
  19.  
  20. $titles = $html->find("a[class=in-match]"); // 1 per match
  21. $result = $html->find("td[class=h-text-center]/a"); // 1
  22. $best_bets = $html->find("td[class=table-matches__odds colored]/span/span/span"); // 1
  23. $odds = $html->find("td[class=table-matches__odds]"); // 2
  24.  
  25. function print_odd($odd) {
  26.     if (array_key_exists('data-odd', $odd->attr)) {
  27.         return $odd->attr['data-odd'];
  28.     }
  29.  
  30.     return $odd->children(0)->children(0)->children(0)->attr['data-odd'];
  31. }
  32.  
  33. $c=0; $b=0; $o=0; $z=0; // two counters
  34. foreach ($titles as $match) {
  35.     list($num1, $num2) = explode(':', $result[$c++]->innertext); // <- explode
  36.     $num1 = intval($num1);
  37.     $num2 = intval($num2);
  38.     $num3 = ($num1 + $num2);
  39.     if ($num3 > 1) {
  40.         $over15 = "OK";
  41.     } else {
  42.         $over15 = "NO";
  43.     }
  44.  
  45.     echo "<tr><td class='rtitle'>".
  46.         "<td class='first-cell'>".$match->innertext."</td> ".            
  47.         "<td>".$match->innertext."</td><td> ".$num1.'</td><td> : </td><td>'.$num2 .  " / " .  // <- example use
  48.         "<td class='first-cell'>".$num3 ."</td> "  .
  49.         "<td class='first-cell'>".$over15 ."</td> "  .
  50.         "<td class='odds'>".print_odd($odds[$b++]) . ";" .
  51.             "".print_odd($odds[$b++]) . ";" .
  52.             "".print_odd($odds[$b++]) . "</td>" .
  53.             "</td></tr><br/>";
  54.  
  55.  
  56.     $servername = "xx.xxx.xxx.xxx";
  57.     $username = "xxx";
  58.     $password = "xxx";
  59.     $dbname = "xxxxxxxx";
  60.  
  61.     // Create connection
  62.     $conn = new mysqli($servername, $username, $password, $dbname);
  63.     // Check connection
  64.     if ($conn->connect_error) {
  65.         die("Connection failed: " . $conn->connect_error);
  66.     }
  67.  
  68.     $sql = "INSERT INTO risultati (titles, scorehome, scoreaway, best_bets)
  69.    VALUES ('$match', '$num1', '$num2', '$odds');";
  70.  
  71.    if ($conn->multi_query($sql) === TRUE) {
  72.     echo "New records created successfully";
  73. } else {
  74.     echo "Error: " . $sql . "<br>" . $conn->error;
  75. }
  76.  
  77. $conn->close();
  78.            
  79.            
  80. $conn = new mysqli($servername, $username, $password, $dbname);
  81. // Check connection
  82. if ($conn->connect_error) {
  83.     die("Connection failed: " . $conn->connect_error);
  84. }
  85.  
  86.     }          
  87.            
  88.  
  89. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement