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

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示