我想解析以下链接中的数据。 的 http://www.dsebd.org/news_archive_7days.php 这是我的任务代码。我想以表格形式显示数据。请帮我找出我错的地方
<?php
include_once('simple_html_dom.php');
$url = "http://www.dsebd.org/news_archive_7days.php/";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$tables = $dom->getElementsByTagName('tr');
foreach($tables as $div) {
if ($div->getElementsByTagName('td') ) {
echo $div ->nodeValue;
}
?>
答案 0 :(得分:1)
看看这段代码(请注意,我添加了一条删除水平线以便于处理的行):
// added this line
$html = str_replace('<tr>
<td colspan="2" bgcolor="#DDDDDD"><span class="style1"><hr></span></td>
</tr>', '', $html);
$dom = new DOMDocument();
@$dom->loadHTML($html);
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item(0)->getElementsByTagName('tr');
$result = array();
$i = 0;
foreach ($rows as $row) {
$i++;
if($i < 4){
continue;
}
$cols = $row->getElementsByTagName('td');
$item = $cols->item(1);
if($item){
if($i % 4 === 0){
$tmp = array();
$tmp['trading_code'] = $item->nodeValue;
}elseif($i % 4 === 1){
$tmp['title'] = $item->nodeValue;
}elseif($i % 4 === 2){
$tmp['news'] = $item->nodeValue;
}elseif($i % 4 === 3){
$tmp['post_date'] = $item->nodeValue;
$result[] = $tmp;
}
}
}
echo '<pre>';
print_r($result);
echo '</pre>';
输出是这样的:
Array
(
[0] => Array
(
[trading_code] => SHY
[title] => DSENEWS: Withdrawal of Authorized Representative
[news] => Withdrawal of Authorized Representative: Shyamol Equity Management Limited, DSE TREC No. 3, has withdrawn one of its Authorized Representatives, Mr. Mohammad Hannan, with immediate effect.
[post_date] => 2016-03-10
)
[1] => Array
(
[trading_code] => EXCH
[title] => DSENEWS: Daily Turnover
[news] => Today's (10.03.2016) Total Trades: 95,239; Volume: 111,966,997 and Turnover: Tk. 3,834.00 million.
[post_date] => 2016-03-10
)
[2] => Array
(
[trading_code] => EXCH
[title] => DSE News: Monthly Review
[news] => The Monthly Review ÃÂ February 2016 has been published. Investors and any other interested person may collect copies of the same from DSE Reception or DSE Sales Center at 9/F and 9/E (7th Floor), Motijheel C/A, Dhaka-1000 respectively. Mob: 01713-425810, Ph: 9564601, 9576210-18 Ext-106, 188, 249. This book is also available at DSE Chittagong Office at Shafi Bhaban (2nd Floor), Agrabad C/A, Chittagong, Sylhet Office at RN Tower (5th & 6th Floor), Chowhatta, Sylhet-3100.
[post_date] => 2016-03-10
)
[3] => Array
(
[trading_code] => DUTCHBANGL
[title] => DUTCHBANGL: Spot for AGM
[news] => Trading of the shares of the Company will be allowed only in the Spot Market and Block transactions will also be settled as per spot settlement cycle with cum benefit from 13.03.2016 to 14.03.2016. Trading of the shares of the Company will remain suspended on record date i.e., 15.03.2016.
[post_date] => 2016-03-10
)
[4] => Array
(
[trading_code] => UCB
[title] => UCB: Spot for AGM
[news] => Trading of the shares of the Company will be allowed only in the Spot Market and Block transactions will also be settled as per spot settlement cycle with cum benefit from 13.03.2016 to 14.03.2016. Trading of the shares of the Company will remain suspended on record date i.e., 15.03.2016.
[post_date] => 2016-03-10
)
// ... and goes on ...
这样您就可以访问特定值:
echo 'Trading code : ' . $result[1]['trading_code'] . '<br>';
echo 'Title : ' . $result[1]['title'] . '<br>';
echo 'News : ' . $result[1]['news'] . '<br>';
echo 'Post Date : ' . $result[1]['post_date'] . '<br>';
输出:
交易代码:EXCH
标题:DSENEWS:每日营业额
新闻:今天(10.03.2016)总交易额:95,239;成交量:111,966,997和营业额:Tk。 3,834.00万。
发布日期:2016-03-10
您可以使用循环,创建表格等。使用您的想象力......
警告:强>
我没有关于该网站关于抓取和机器人的条款的信息,因此使用风险自负!! (您应该查看该问题并了解他们的位置)
答案 1 :(得分:0)
include_once "simple_html_dom.php";
$html = file_get_html('http://www.dsebd.org/news_archive_7days.php/');
foreach($html->find('tr') as $element)
echo $element; '<br>';