dpy15285
dpy15285
2014-04-19 13:48
浏览 47
已采纳

在PHP中使用XPath循环

I have this html table:

<tbody>
   <tr class="r1">

      <td class="l rbrd">
         <img class="spr2 sport sp1" align="absmiddle" src="/s.gif">
      </td>

      <td class="l rbrd">19/4 18:30</td>

      <td class="l rbrd">
         <a title="CHELSEA FC - SUNDERLAND" href="/chelsea-fc-vs-sunderland/e/4509648/" target="_blank">CHELSEA FC - SUNDERLAND</a>
      </td>

      <td class="c w40">
         <span class="o">1,21</span>
         <span class="p">91,8%</span>
      </td>

   </tr>

There are more than one hundred <"tr"> structured at the same way, which contain lots of <"td">. How can i loop with xpath to store all data in a database?

This is my php code, but i can not go on.. help!

<?php

$curl = curl_init('http://www.oxybet.ro/pariu/external/betfair-volumes.htm');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10');
$html = curl_exec($curl);
curl_close($curl);

if (!$html) {
    die("something's wrong!");
}

$dom = new DOMDocument();
@$dom->loadHTML($html);

$xpath = new DOMXPath($dom);

$scores = array();

$tableRows = $xpath->query('//div//div//div[2]//div/div//table');

foreach ($tableRows->tr as $row) {

   $td = $xpath->query('td', $row);
    // we'll store information about each match in this array
    $match = array();
...

}

Final result:

[0] => Array
        (
            [date] => 18:30 19/4
            [teams] => CHELSEA FC - SUNDERLAND
            [1] => 1,21
            [1 volumes] => 91,8%

        )
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drsb77336
    drsb77336 2014-04-19 14:29
    已采纳

    You can try the following approach.

    <?php
    $url = 'http://www.oxybet.ro/pariu/external/betfair-volumes.htm';
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $response = curl_exec($ch);
    
    curl_close($ch);
    
    $document = new DOMDocument();
    $document->loadHTML($response);
    
    $xpath = new DOMXPath($document);
    $expression = '/html/body/div/div/div[2]/div/div/table/tbody/tr';
    $rows = $xpath->query($expression);
    
    $results = array();
    
    foreach ($rows as $row) {
      $result = array();
    
      $expression = './td[2]';
      $result['date'] = $xpath->query($expression, $row)->item(0)->nodeValue;
    
      $expression = './td[3]/a';
      $result['teams'] = $xpath->query($expression, $row)->item(0)->nodeValue;
    
      $expression = './td[4]/span';
      $result['1'] = $xpath->query($expression, $row)->item(0)->nodeValue;
    
      $expression = './td[4]/span[2]';
      $result['1 volumes'] = $xpath->query($expression, $row)->item(0)->nodeValue;
    
      array_push($results, $result);
    }
    
    var_dump($results);
    
    点赞 评论

相关推荐