1
  <td class="cinetime">
    <div>Screen: 3</div>
    <br clear="all">
    <span>1:00</span>
    <span>11:00</span>
    <span>13:00</span>
    <span>15:00</span>
    <br clear="all">
    <div>Screen: 4</div>
    <br clear="all">
    <span>12:05</span>
    <span>14:05</span>
    <span>16:05</span>
    <span>18:05</span>
    <span>20:05</span>
    <div>Screen: 3 (3D)</div>
  </td>

以上是我正在编写的 HTML。

我想分别<span>屏幕 3屏幕 4获取数据。

这是我下面的代码,但它从所有<span>s中获取数据

foreach($cinema->find("td.cinetime span") as $times) {
    echo $times->plaintext;
}
4

2 回答 2

0

像这样的东西:

$dom = new simple_html_dom();
$dom->load($source);


$cinetime = $dom->find('.cinetime',0);

$screens = array();
$screenName = '';

foreach($cinetime->children() as $elem){
    if($elem->tag == 'div'){
        $screenName = $elem->innertext;
    } else
    if($elem->tag == 'span'){
        $screens[$screenName][] = $elem->innertext;
    }
}
print_r($screens);

这产生了这个:

Array
(
[Screen: 3] => Array
    (
        [0] => 1:00
        [1] => 11:00
        [2] => 13:00
        [3] => 15:00
    )

[Screen: 4] => Array
    (
        [0] => 12:05
        [1] => 14:05
        [2] => 16:05
        [3] => 18:05
        [4] => 20:05
    )

)

?

于 2012-10-26T14:43:21.933 回答
0

您需要检查 td.cintime 中的所有元素并检查文本是否是您需要的(如 Screen: 3),然后检查其他元素是否为 span,然后将其放入某个数组中,直到获得另一个 div。这意味着一个屏幕块已经结束。重复直到你得到你需要的一切。

伪代码:

$screensData = array();
$start = false;
foreach($cinema->find("td.cintime *") as $times) {
   if($times->tag == 'div') {
      $start = false;
      if($times->plaintext == 'Screen: 3') {
         $screensData[3] = array();
         $start = 3;
      }
   }

   if($start && $times->tag == 'span') {
      $screensData[$start][] = $times->plaintext;
   }
}
于 2012-10-26T12:14:48.740 回答