可否帮忙写一个单页页的PHP采集程序,并附上实例
比方说,我要采集这个页面:http://news.163.com/12/0613/20/83TJ7PA700014JB6.html
要求:
采集标题
采集正文
谢谢!
------解决方案--------------------
首先去http://simplehtmldom.sourceforge.net/index.htm(点击Download latest version form Sourceforge.)下载一个simple_html_dom.php,傻瓜式的正则,另官网上有详细教程,很容易看懂。
<br />header("Content-type: text/html; charset=gb2312");<br />require dirname(__FILE__) . '/simple_html_dom.php';<br />$ch = curl_init();<br />curl_setopt($ch, CURLOPT_URL, 'http://news.163.com/12/0613/20/83TJ7PA700014JB6.html');<br />curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br />curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');<br />$htmls = curl_exec($ch);<br />curl_close($ch);<br />$html = str_get_html($htmls);<br />foreach($html->find('#h1title') as $title){ <br /> echo strip_tags($title).'<br />';//标题<br />}<br />foreach($html->find('#endText') as $content){ <br /> echo strip_tags($content);//正文<br />}