
正文
php采集网页商品数据库 php数据采集方法
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
抓取网页数据怎么保存到数据库 php
给一个例子你看看吧.
if($pro_list_contents=@file_get_contents(''))
{
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; $icount($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(''.$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 );
@copy( $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="', $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";
}
}
相关问答
Q1: PHP ,采集到的数据,如何分组放入数据库里面?
愚见php采集网页商品数据库:
用函数explode(",",$hq_str_sh601006)
能把字符串按照逗号分开。可以直接赋值给一个数组变量。
如:$hq_str_arr=explode(",",$hq_str_sh601006);
然后php采集网页商品数据库你自己可以从数组中按照你获取php采集网页商品数据库的顺序给数组中相应的元素赋值给数据库的对应字段。
希望有帮助。
Q2: php用正则采集到多个数据如何写入数据库 现在我用php正则采集到了,商品名称title:1,2,3,...n;商品id...
个人建议采集到的数据存储为二维数组,其中商品id是唯一的,所以将id作为键值,然后每个键值对应的是一个一次包含title,price等数据的二维数组,这样采集完成后,可以将这个二维数组遍历循环插入数据库,这样也不容易出现错误
比如其中一个商品id为1,标题为“牙刷”,价格为$2,就这样写入数组$arr[1]=array("牙刷","$2")
Q3: 怎样借助PHP从HTML网页中获取phpmyadmin数据库里数据表的内容
?php
$link=mysql_connect('localhost','用户名','密码')or die("数据库连接失败");//连接数据库
mysql_select_db('数据库名',$link);//选择数据库
mysql_query("set names utf8");//设置编码格式
$q="select * from "数据表";//设置查询指令
$result=mysql_query($q);//执行查询
while($row=mysql_fetch_assoc($result))//将result结果集中查询结果取出一条
{ echo 返回到HTML; }
?
html界面使用ajax的成功返回值,再渲染在界面里就行了
Q4: 你好,你有抓取网页数据到数据库的PHP代码么?
?php
//设置连接
$DBserver
=
"localhost";
$DBname
=
"数据库";
$DBuser
=
"账号";
$DBpassword
=
"密码";
$con
=
mysql_connect("localhost","账号","密码");
mysql_select_db("数据库");
$contents
=
file_get_contents($url);
//$contents就是网页内容php采集网页商品数据库,$url就是链接
$contents
=
mysql_real_escape_string($contents);
//转义php采集网页商品数据库,不用可以不要
$SQL="
INSERT
INTO
数据库表(数据字段)
VALUES('{$contents}')";
mysql_query($SQL)
or
die(mysql_error());
?
Q5: PHP获取网站中的信息并存入数据库
用PHP自带函数就可以实现,首先要过去对方的网页信息,用
file_get_contents();参数是对方的URL地址,这个函数返回是一个字符串你想要的东西就在这个字符串中了
接下来就可以针对这个字符串做处理了,说下思路,正如你这个问题想获取到航班号起飞时间,在这个网页中应该有很多相同的标签元素,它们都有共同点,用
用正则表达式preg_match();或者是
preg_match_all();这两个函数它们都返回一个数组,这个数组存的就是你要的航班号和起飞时间,那么相同信息的数组就会出现了,然后在对这个数组进行分析找到你要的某个值或全部的值
获取信息要用到的3个函数是:
file_get_contents();
preg_match();
preg_match_all();
关于php采集网页商品数据库和php数据采集方法的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







