日期:2014-05-17  浏览次数:20501 次

抓取的网页如何存入mysql数据库
写的一个PHP代码(test.php):

<?php?
$url = "http://www.baidu.com/";?
$contents = file_get_contents($url);?
echo $contents;?>?

如何把抓取的这个网页数据mysql数据库中呢? 表是Page 字段1:Pageid | 字段2:Pagetext


求代码

------解决方案--------------------
不就是一句 insert into 了?
值都有了,字段也有了。。。
------解决方案--------------------
若果pageid是自增的。则空缺也可。


$sql="insert into `Page` values ('','$contents')";
------解决方案--------------------
preg_match_all('/<h1>(.*?)<\/table>/is',$str,$match); //$str换成你自己的字符串。
print_r($match);
------解决方案--------------------
PHP code

$contents = file_get_contents('a.php');
preg_match_all('/(<h1.*<\/table>)/iUs', $contents, $match);
//如果有多个需要匹配的结果 自己输出 match数组 组织成字符串...
$contents = $match[1][0];
mysql_connect('localhost', 'root', '');
mysql_select_db("lookdb");
mysql_query("SET NAMES 'GBK'");
$SQL = "INSERT INTO page (pagetext) VALUES('{$contents}')";
mysql_query($SQL);