天天看點

php實作抓取網站百度快照和百度收錄數量的代碼執行個體

如果大家稍加仔細看一下就能發現一個問題,我們下面幾個抓取百度收錄或快照或熱詞的程式中都有一個函數file_get_contents(),他是php采集網頁常用的哦。

代碼如下

複制代碼

<?

/*

抓取百度收錄代碼

*/

function baidu($s){

$baidu="http://www.baidu.com/s?wd=site%3A".$s;

$site=file_get_contents($baidu);

//$site=iconv("gb2312", "UTF-8", $site);

ereg("找到相關網頁(.*)篇,", $site,$count);

$count=str_replace("找到相關網頁","",$count);

$count=str_replace("篇,","",$count);

$count=str_replace("約","",$count);

$count=str_replace(",","",$count);

return $count[0];

}

echo baidu(www.3ppt.com); //擷取好主題在百度中的收錄數量

?>

擷取百度的熱詞

<?php

/**

* * @user 小傑

* @return array 傳回百度的熱詞資料(數組傳回)

*/

function getBaiduHotKeyWord()

{

$templateRss = file_get_contents('http://top.baidu.com/rss_xml.php?p=top10');

If (preg_match('/&lt;table>(.*)</table>/is', $templateRss, $_description)) {

$templateRss = $_description [0];

$templateRss = str_replace("&", "&", $templateRss);

}

$templateRss = "<?xml version="1.0" encoding="GBK"?>" . $templateRss;

$xml = simplexml_load_String($templateRss);

foreach ($xml-&gt;tbody-&gt;tr as $temp) {

if (!empty ($temp-&gt;td-&gt;a)) {

$keyArray [] = trim(($temp-&gt;td-&gt;a));

return $keyArray;

print_r(getBaiduHotKeyWord());

這是在網上找的 稍微修改了下 将下面代碼寫入php檔案

百度收錄和百度快照時間