php抓取数据
Ⅰ 请问php怎样抓取其它网站的动态数据,显示在自己的网页内并同步更新。
刚吃完午饭吧,来帮你实现一下吧。记得加分哦。
$url = "http://www.boc.cn/sourcedb/whpj/";
$queryServer = curl_init();
curl_setopt($queryServer, CURLOPT_URL, $url);
curl_setopt($queryServer, CURLOPT_HEADER, 0);
curl_setopt($queryServer, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($queryServer, CURLOPT_RETURNTRANSFER, true);
curl_setopt($queryServer, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($queryServer, CURLOPT_TIMEOUT, 30);
$html = curl_exec($queryServer);
$html = iconv('UTF-8','GBK//IGNORE',$html); //如果你需要是的数据是utf-8编码的,这一行可以注销,如果需要gbk编码的,请保留.如果出现乱码,就是一行的问题,你自己调着试吧
//echo $holder;exit; 此处可以输出来测试.
$html = str_replace(array("\n","\r","\t"),"",$html);
$preg = '/<table\s+width=\"800\"[^>]+>(.*?)<\/table>/';
preg_match_all($preg,$html,$out);
//匹配每行
preg_match_all('/<tr[^>]+>(.*?)<\/tr>/',$out[1][0],$tr);
//匹配每个td
$result = array();
$match = '/<td.+>([^<]+)<\/td>/U';
foreach( $tr[0] as $key => $value ){
preg_match_all($match,$value,$arr);
$result[] = $arr[1];
}
//输出测试,$result就是你要的数据,至于你要怎么输出显示格式,那就随心调就好了。
foreach( $result as $key => $value ){
echo implode("\t",$value);
echo "<br>";
}
exit;
Ⅱ 使用PHP的cURL库进行网页抓取
使用模明仿PHP的cURL库可以简单和有效地去抓网页 你只需要运行一个脚本 然后分析一下你所抓取的网页 然后就可以以程序的方式得到你想要的数据了 无论是你想从从一个链接上取部分数据 或是取一个XML文件并把其导入数据库 那怕就是简单的获取网页内容 cURL 是一个功能强大的PHP库 本文主要讲述如果使用这个PHP库
启用 cURL 设置
首先 我们得先要确定我们的PHP是否开启了这个库 你可以通过使用php_info()函数来得到这一信息
﹤?phpphpinfo();?﹥
如果你可以在网页上看到下面的输出 那么表示cURL库已被开启
如果你看到的话 那么你需要设置你的PHP并开启这个库 如果你是在Windows平台下 那么非常简单 你需要改一改你的php ini文件的设置 找到php_curl dll 并取消前面的分号注释就行了 如下所示
//取消下在的注释extension=php_curl dll
如果你旦纤是在Linux下面 那么 你需要重新编译你的PHP了 编辑时 你需要打开编译参数——在configure命令上加上 –with curl 参数
一个小示例
如果一切就绪 下面是一个小例程
﹤?php// 初始化一个 cURL 对象$curl = curl_init();
// 设置你需要抓取的URLcurl_setopt($curl CURLOPT_URL //cocre );
// 设置headercurl_setopt($curl CURLOPT_HEADER );
// 设置cURL 参数 要求结果保存到字符串中还是输出到屏幕上槐链 curl_setopt($curl CURLOPT_RETURNTRANSFER );
// 运行cURL 请求网页$data = curl_exec($curl);
// 关闭URL请求curl_close($curl);
// 显示获得的数据var_mp($data);
如何POST数据
上面是抓取网页的代码 下面则是向某个网页POST数据 假设我们有一个处理表单的网址// example /sendSMS php 其可以接受两个表单域 一个是电话号码 一个是短信内容
﹤?php$phoneNumber = ;$message = This message was generated by curl and php ;$curlPost = pNUMBER= urlencode($phoneNumber) &MESSAGE= urlencode($message) &SUBMIT=Send ;$ch = curl_init();curl_setopt($ch CURLOPT_URL // example /sendSMS php );curl_setopt($ch CURLOPT_HEADER );curl_setopt($ch CURLOPT_RETURNTRANSFER );curl_setopt($ch CURLOPT_POST );curl_setopt($ch CURLOPT_POSTFIELDS $curlPost);$data = curl_exec();curl_close($ch);?﹥
从上面的程序我们可以看到 使用CURLOPT_POST设置HTTP协议的POST方法 而不是GET方法 然后以CURLOPT_POSTFIELDS设置POST的数据
关于代理服务器
下面是一个如何使用代理服务器的示例 请注意其中高亮的代码 代码很简单 我就不用多说了
﹤?php $ch = curl_init();curl_setopt($ch CURLOPT_URL // example );curl_setopt($ch CURLOPT_HEADER );curl_setopt($ch CURLOPT_RETURNTRANSFER );curl_setopt($ch CURLOPT_HTTPPROXYTUNNEL );curl_setopt($ch CURLOPT_PROXY fakeproxy : );curl_setopt($ch CURLOPT_PROXYUSERPWD user:password );$data = curl_exec();curl_close($ch);?﹥ 关于SSL和Cookie
关于SSL也就是HTTPS协议 你只需要把CURLOPT_URL连接中的//变成//就可以了 当然 还有一个参数叫CURLOPT_SSL_VERIFYHOST可以设置为验证站点
关于Cookie 你需要了解下面三个参数
CURLOPT_COOKIE 在当面的会话中设置一个cookie
CURLOPT_COOKIEJAR 当会话结束的时候保存一个Cookie
CURLOPT_COOKIEFILE Cookie的文件
HTTP服务器认证
最后 我们来看一看HTTP服务器认证的情况
﹤?php $ch = curl_init();curl_setopt($ch CURLOPT_URL // example );curl_setopt($ch CURLOPT_RETURNTRANSFER );curl_setopt($ch CURLOPT_HTTPAUTH CURLAUTH_BASIC);curl_setopt(CURLOPT_USERPWD [username]:[password] )
$data = curl_exec();curl_close($ch);?﹥
关于其它更多的内容 请参看相关的cURL手册 lishixin/Article/program/PHP/201311/21491
Ⅲ 抓取网页数据怎么保存到数据库 php
给一个例子你看看吧.
if($pro_list_contents=@file_get_contents('http://www.phoenix-luxury.com/louis-vuitton-c-82.html'))
{
preg_match_all("/<td width=\"50%\" valign=\"top\">(.*)<td width=\"10\"><img src=\"images\/spacer.gif\"/isU", $pro_list_contents, $pro_list_contents_ary);
for($i=0; $i<count($pro_list_contents_ary[1]); $i++)
{
preg_match_all("/<a href=\"(.*)\"><img src=\"(.*)\".*<span>(.*)<\/span>/isU", $pro_list_contents_ary[1][$i], $url_img_price);
$url=addslashes($url_img_price[1][0]);
$img=str_replace(' ', '20%', trim('http://www.phoenix-luxury.com/'.$url_img_price[2][0]));
$price=(float)str_replace('$', '', $url_img_price[3][0]);
preg_match_all("/<a class=\"ml1\" href=\".*\">(.*)<\/a>/isU", $pro_list_contents_ary[1][$i], $proname_ary);
$proname=addslashes($proname_ary[1][0]);
include("inc/db_connections.php");
$rs=mysql_query("select * from pro where Url='$url' and CateId='{$cate_row['CateId']}'"); //是否已经采集了
if(mysql_num_rows($rs))
{
echo "跳过:{$url}<br>";
continue;
}
$basedir='/u_file/pro/img/'.date('H/');
$save_dir=Build_dir($basedir); //创建目录函数
$ext_name = GetFileExtName( $img ); //取得图片后辍名
$SaveName = date( 'mdHis' ) . rand( 10000, 99999 ) . '.' . $ext_name;
if( $get_file=@file_get_contents( $img ) )
{
$fp = @fopen( $save_dir . $SaveName, 'w' );
@fwrite( $fp, $get_file );
@fclose( $fp );
@chmod( $save_dir . $SaveName, 0777 );
@( $save_dir . $SaveName, $save_dir . 'small_'.$SaveName );
$imgpath=$basedir.'small_'.$SaveName;
}
else
{
$imgpath='';
}
if($pro_intro_contents=@file_get_contents($url))
{
preg_match_all("/<\/h1>(.*)<\/td><\/tr>/isU", $pro_intro_contents, $pro_intro_contents_ary);
$p_contents=addslashes(str_replace('src="', 'src="http://www.phoenix-luxury.com', $pro_intro_contents_ary[1][0]));
$p_contents=SaveRemoteImg($p_contents, '/u_file/pro/intro/'.date('H/')); //把远程html代码里的图片保存到本地
}
$t=time();
mysql_query("insert into pro(CateId, ProName, PicPath_0, S_PicPath_0, Price_0, Contents, AddTime, Url) values('{$cate_row['CateId']}', '$proname', '$imgpath', '$img', '$price', '$p_contents', '$t', '$url')");
echo $url.$img.$cate."<br>\r\n";
}
}