用php构建基于baidu的关键字搜索 - Aiting's Blog-艾丁堡 - DonewsBlog
;test.php 从baidu的搜索页面提取需要的信息集中存入数据库
;-----------------------------------
<?php
include ("config.php");
for($j=1;$j<=8;$j++)
{
$pn=($j-1)*100;
$url = "http://www.baidu.com/s?lm=0&si=&rn=100&ie=gb2312&ct=0&wd=%C5%A9%D2%B5%D0%C5%CF%A2&pn=".$pn."&cl=3";
$filefrom = implode ('', file($url));
$filefrom = strstr($filefrom,'<ol>');
$filefrom2 = strstr($filefrom,'</ol>');
$filefrom3 = explode($filefrom2,$filefrom);
$filefrom = $filefrom3[0];
//以上是截取<ol></ol>之间信息的代码,有点乱
$xx1 = '<p class=p1>';
$xx2 = '<br>';
$xx = explode($xx1,$filefrom);
for($i=1;$i<=100;$i++)
{
$ii=$i+($j-1)*100;
$every_item = explode($xx2,$xx[$i]);
$url = explode('/',$every_item[4]);
$url = substr($url[0],20);
echo '<p>'.$ii.')'.'<p>';
echo $every_item[0].'<br>';
echo $every_item[4].'<br>';
echo $every_item[5].'<br>';
echo 'http://'.$url;
echo '<p>';
$url = 'http://'.$url;
$title = $every_item[0];
$content = "";
$keywords = "农业信息";
$hits = 1;
$buff = substr($url,11);
$buff1 = substr($url,7);
if($buff != "http://www.")
{
$first_chat = substr($buff,0,1);
}
else
{
$first_chat = substr($buff1,0,1);
}
echo "首字母".$first_chat;
/*
$result=mysql_query("
INSERT INTO `test_test1` ( `id` , `url` , `title` , `content` , `rank` , `time` , `first_chat` , `hits` )
VALUES (
'', '$url', '$title', '$content', '农业信息', Now(), '$first_chat', '1'
);");
echo "写入了一条新信息";*/
}
}?>
;test2.php 生成execl表格
<?php
// 转载请注明phpteam
$title = "数据库名:test, 数据表:test_test1, 备份日期:" . date("Y-m-d H:i:s");
$sep = "\t";
$crlf = "\n";
$conn = @mysql_connect("xxx","xxx","xxx") or die("不能连接数据库");
@mysql_select_db("test", $conn);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $title . $crlf . $crlf;
$query = "select * from test_test1";
$result = mysql_query($query) or die(mysql_error());
$fields = mysql_num_fields($result);
for($i = 0; $i < $fields; $i++) {
echo mysql_field_name($result, $i) . $sep;
}
echo $crlf;
while($row = mysql_fetch_row($result)) {
$line = "";
for($i = 0; $i<$fields; $i++) {
$line .= $row[$i] . $sep;
}
echo $line . $crlf;
}
?>