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 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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!