目前我正在尝试使用xpath从网站解析html页面。
我需要获得以下格式的结果:
日期
节目时间:节目名称
例如:
OCT 18
1.00AM:Ye Hai Mohabbatein
我使用以下代码来获取此信息。但它正在工作2次。
<?php
$dat="Oct 18";
$ch = curl_init('http://www.starplus.in/schedule.aspx');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($page);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$n=0;
$shows = array();
$tables1 = $xpath->query("//td[@class='bdr_R_dot']");
$tables2 = $xpath->query("//div[@class='sech_div_bg']/table");
foreach ($tables1 as $table1) {
$date = $xpath->query('./span[2]', $table1)->item(0)->nodeValue;
if($date==$dat){
echo "<h1>$date</h1> <br>";
foreach ($tables2 as $table2) {
$time_slot = $xpath->query('./tr[1]/td/span', $table2)->item(0)->nodeValue;
$show_name = $xpath->query('./tr[3]/td/span', $table2)->item(0)->nodeValue;
$shows[] = array('time_slot' => $time_slot, 'show_name' => $show_name);
echo "$time_slot - $show_name <br/>";
}
}
}
?>
我做了很多事情,但我的代码执行了2次,整个数据打印了两次。如果有人在这个问题上帮助我,我将感激不尽。
答案 0 :(得分:1)
是的,您可以使用该日期来获取当天的节目。您可以将其用作特定行表的指针。
第一个目标,它将落在哪一行,然后获取这些行。例如:
$dat = "Oct 18";
$ch = curl_init('http://www.starplus.in/schedule.aspx');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($page);
libxml_clear_errors();
$xpath = new DOMXPath($dom);
$shows = array();
$node_list = $xpath->query("
//td[@class='bdr_R_dot']/span[text() = '$dat']
/parent::td/following-sibling::td
/table/tr/td[3]/div/ul/li
");
echo $dat . '<br/><br/>';
foreach ($node_list as $el) {
$time_slot = $xpath->query('./div/table/tr[1]/td/span', $el)->item(0)->nodeValue;
$show_name = $xpath->query('./div/table/tr[3]/td/span', $el)->item(0)->nodeValue;
echo "$time_slot : $show_name <br/>";
}