php抓取网页写数据库 php获取网站html代码( 二 )


怎么用php程序将页面传过来的数据写入数据库? 代码如下:就是$insert这条SQL语句的问题
echo$insert;看看语句有没有问题
检查方法将输出的SQL语句放在数据库里执行以下
标准写法
$insert = "insert into my_tab(user,psd) values('".$user."','".$psd."')":
你好,你有抓取网页数据到数据库的PHP代码么??php
//设置连接
$DBserver
=
"localhost";
$DBname
=
"数据库";
$DBuser
=
"账号";
$DBpassword
=
"密码";
$con
=
mysql_connect("localhost","账号","密码");
mysql_select_db("数据库");
$contents
=
file_get_contents($url);
//$contents就是网页内容 , $url就是链接
$contents
=
mysql_real_escape_string($contents);
//转义 , 不用可以不要
$SQL="
INSERT
INTO
数据库表(数据字段)
VALUES('{$contents}')";
mysql_query($SQL)
or
die(mysql_error());
?
抓取网页数据怎么保存到数据库 php给一个例子php抓取网页写数据库你看看吧.
if($pro_list_contents=@file_get_contents(''))
{
preg_match_all("/td width=\"50%\" valign=\"top\"(.*)td width=\"10\"img src=https://www.04ip.com/"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=https://www.04ip.com/"(.*)\"img src=https://www.04ip.com/"(.*)\".*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=https://www.04ip.com/".*\"(.*)\/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']}'"); //是否已经采集php抓取网页写数据库了
if(mysql_num_rows($rs))
{
echo "跳过php抓取网页写数据库:{$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代码里php抓取网页写数据库的图片保存到本地
}
$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";
}
}
怎样借助PHP从HTML网页中获取phpmyadmin数据库里数据表的内容?你好 , 我想到了两种方法:
第一种:可以使用js脚本调用php,PHP查询后返回给js,js再把内容替换到某个idv
第二种:直接用php来做 , 不需要js和html,php先输出html头 , 再查询sql,并输出,再输出html别的内容或者结束语句

推荐阅读