我自己编了一段简单的php代码,实现如下功能:采集www.weather.com.cn的一个报道杭州天气的页面,并原样输出,可就是采集不到,,,不知是什么原因,大家帮分析下代码如下:
<?php
$url="http://www.weather.com.cn/html/weather/101210101.shtml";
$fcontents=file_get_contents($url);
echo $fcontents;
?>我把上面的代码中的http://www.weather.com.cn/html/weather/101210101.shtml换成 http://ent.163.com/09/0511/10/591CBR5C00031NJO.html采集网易的一个新闻页面却成功了,搞不懂是怎么回事??大家帮分析下

解决方案 »

  1.   

    最开始以为是php不能采集.shtml后缀的页面,因为第一个是.shtml的后缀,第二个却不是,是.html的但后来采集sina 的一个页面,也是.shtml后缀的,却成功了,狂晕之中,
      

  2.   

    采集时,加上 User-Agent 头即可.
    <?php 
    $context=array('http' => array ('header'=> 'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)', ),);
    $xcontext = stream_context_create($context);$url="http://www.weather.com.cn/html/weather/101210101.shtml"; 
    $fcontents=file_get_contents($url, FALSE, $xcontext); 
    echo $fcontents; 
    exit;
    ?>