php抓取网页数据的技术要点
				
									
					
					
						|  | 
							admin 2013年2月25日 23:46
								本文热度 6647 | 
					
				 
				
php中一般都是去抓取网页数据?如想把http://mp3.baidu.com/ 歌曲500TOP把抓下来,如何实现?
    
该文章在 2013/2/25 23:46:42 编辑过
|  |  | 
| 全部评论1 | 
	|  | admin 2013年2月25日 23:52 
			
问题中提到需要用PHP实现,个人总结整理了一下,有以下几种常用的用php抓取网页中的内容的方法,供您参考。 1.使用file_get_contents    <?php       $url = "http://www.34ways.com";    $contents = file_get_contents($url);    //如果出现中文乱码使用下面代码    //$getcontent = iconv("gb2312", "utf-8",$contents);     echo $contents;    ?>
 2.使用curl    <?php       $url = "http://www.34ways.com";    $ch = curl_init();    $timeout = 5;    curl_setopt($ch, CURLOPT_URL, $url);    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);    //在需要用户检测的网页里需要增加下面两行    //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);    //curl_setopt($ch, CURLOPT_USERPWD, US_NAME.":".US_PWD);    $contents = curl_exec($ch);    curl_close($ch);    echo $contents;    ?>
 3.使用fopen->fread->fclose    <?php       $handle = fopen ("http://www.34ways.com", "rb");    $contents = "";    do {       $data = fread($handle, 1024);       if (strlen($data) == 0) {       break;       }       $contents .= $data;    } while(true);    fclose ($handle);    echo $contents;    ?>
 最后提醒几点:1.使用file_get_contents和fopen必须空间开启allow_url_fopen。
 方法:
 
编辑php.ini,设置allow_url_fopen =On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。
 2.使用curl必须空间开启curl。方法:
 
windows下修改php.ini,将extension=php_curl.dll前面的分号去掉,而且需要拷贝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安装curl扩展。 评论 (1) • 链接 • 2012-07-10  
先file_get_contents("http://mp3.baidu.com/"),取一个html的结果 然后从html 利用正则找规律 来取 页面只给了ID, 由flash查找的.如果想知道怎么查找的,可以反编译SWF.有其它的音乐网站列出了baidu的排行榜,也许从那里抓更容易.
 只是举了个例子,抓去网页数据无非就两步:1、抓取;2:分析抓取分几种情况:1、普通字符串,就是普通网页源码,用file_get_contents或curl抓取,2、有序数据如xml,可以用simplexml_load_file抓取
 分析:如果是第二种情况,就按照xml数据来遍历处理好了。 如果是第一种情况,用preg_match来匹配目标数据
 
 
只说下思路,这样的代码网上太多,就不多说了。PHP实现的话,file_get_contents网页内容下来,然后用正则进行分析得到mp3文件地址到一个文件里面,然后用迅雷批量下载。
 PHP下载mp3 - -| 你懂的
 
<?php/*
 * 如下: 方法有点笨
 * 抓取网页内容用 PHP 的正则
 * 用JS每隔5分钟刷新当前页面---即重新获取网页内容
 *
 * 注: $mode中--<title></title>-更改为所需内容(如 $mode = "#<a(.)</a>#";>获取所有链接) * * window.location.href="http://localhost/baidu/refesh.php";中的http://localhost/baidu/refesh.php * 更改为自己的URL----作用:即刷新当前页面 * * setInterval("ref()",300000);是每隔300000毫秒(即 5 * 60 *1000 毫秒即5分钟)执行一次函数 ref() * * print_r($arr);输出获得的所有内容 $arr是一个数组 可根据所需输出一部分(如 echo $arr[1][0];) * 若要获得所有内容 可去掉 * $mode = "#<title>(.)</title>#";
 if(preg_match_all($mode,$content,$arr)){
 print_r($arr);
 echo "<br/>";
 echo $arr[1][0];
 }
 再加上 echo $content;
 */
 $url = "http://www.baidu.com"; //目标站
 $fp = @fopen($url, "r") or die("超时");
 $content=file_get_contents($url);$mode = "#<title>(.*)</title>#";
 if(preg_match_all($mode,$content,$arr)){
 //print_r($arr);
 echo "<br/>";
 echo $arr[0];
 }
 ?>
 <script language="JavaScript" type="text/javascript">
 <--
 function ref(){
 window.location.href="http://localhost/baidu/refesh.php";
 }
 setInterval("ref()",300000);
 //-->
 </script>
 详细汇总 http://blog.163.com/kong_qing_feng/blog/static/186228032009418144099/
 |