我正在尝试使用simple-html-dom构建一个scrapper。
每个tr都有“事件列表”类以及唯一ID。一切正常,但我无法弄清楚如何获取和存储id。
这是我的代码:
foreach($html->find('tr[class=event-listing]') as $event)
{
$item['date'] = trim($event->find('td', 0)->plaintext);
$first_trim = ltrim($event->find('td', 1)->plaintext, 'at ,vs.');
$item['opponent'] = rtrim($first_trim, ' * ');
$item['location'] = trim($event->find('td', 2)->plaintext);
$item['time'] = trim($event->find('td', 3)->plaintext);
$sched[] = $item;
}
答案 0 :(得分:3)
您不需要任何特殊的$event->id
可以正常工作
$string = '<tr valign="TOP" bgcolor="#d1d1d1" class="event-listing" title="2012,4,18,21,00,00" id="1444896"> <td class="row-text"> </td> <td class="row-text">vs. Northern Colorado *</td> <td class="row-text">Orem, Utah</td> <td class="row-text">W, 11-6</td> </tr>';
$html = str_get_html($string);
$sched = array();
foreach ( $html->find('tr[class=event-listing]') as $event ) {
$item['date'] = trim($event->find('td', 0)->plaintext);
$first_trim = ltrim($event->find('td', 1)->plaintext, 'at ,vs.');
$item['opponent'] = rtrim($first_trim, ' * ');
$item['location'] = trim($event->find('td', 2)->plaintext);
$item['time'] = trim($event->find('td', 3)->plaintext);
$item['id'] = $event->id;
$sched[] = $item;
}
var_dump($sched);
输出
array
0 =>
array
'date' => string ' ' (length=6)
'opponent' => string 'Northern Colorado' (length=17)
'location' => string 'Orem, Utah' (length=10)
'time' => string 'W, 11-6' (length=7)
'id' => string '1444896' (length=7)
^------------------------------ ID Captured