PHP获取百度收录数量及百度快照

Posted by Harid八月 - 4 - 2011 Leave comments   441 views 

关于PHP,我绝对是门外汉,这里记录一下我晚上为获取百度收录总数量、百度今日收录数量和百度快照日期的而整理的代码。它用到了三个函数:file_get_contents()、preg_match()与iconv()。file_get_contents() 函数把整个文件读入一个字符串中,它是用于将文件的内容读入到一个字符串中的首选方法。preg_match()函数则根据指定的模式来匹配文件名或字符串。iconv()函数用于将字符串由一种编码转换为另一种编码。

具体代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
    $domain = "www.ninthday.net";  /*欲查询的域名*/
    $site_url = 'http://www.baidu.com/s?wd=site%3A';
    $all = $site_url.$domain; /*域名所有收录的网址*/
    $today = $all.'&lm=1';    /*域名今日收录的网址*/
    $utf_pattern = "/找到相关结果数(.*)个/";
    $gb2312_pattern = iconv("UTF-8","GB2312",$utf_pattern); /*因为百度为GB2312编码*/
    $kz_pattern = "/<span class=\"g\">(.*)<\/span>/"; /*用以匹配快照日期的字符串*/
    $times = "/\d{4}-\d{1,2}-\d{1,2}/"; /*匹配快照日期的正则表达式,如:2011-8-4*/
    $s0 = @file_get_contents($all);    /*将site:www.ninthday.net的网页置入$s0字符串中*/
    $s1 = @file_get_contents($today);
    preg_match($gb2312_pattern,$s0,$all_num); /*匹配"找到相关结果数*个"*/
    preg_match($gb2312_pattern,$s1,$today_num);
    preg_match($kz_pattern,$s0,$temp);
    preg_match($times,$temp[0],$screenshot);
    if($all_num[1] == "")
        $all_num[1] = 0;
    if($today_num[1] == "")
        $today_num[1] = 0;
    if($screenshot[0] == "")
        $screenshot[0] = "暂无快照";
?>
<html>
    <head>
    <title>Test</title>
    </head>
<body>
    <p>百度收录:<a href="<?php echo $all; ?>" target="_blank"><?php echo $all_num[1]; ?></a></p>
    <p>百度今日收录:<a href="<?php echo $today; ?>" target="_blank"><?php echo $today_num[1]; ?></a></p>
    <p>百度快照日期:<a href="<?php echo $all; ?>"><?php echo $screenshot[0]; ?></a></p>
</body>
</html>

 

下图是运行后的显示结果:

百度收录及快照

   声明:本文采用 BY-NC-SA 协议进行授权 | 星期九
   原创文章转载请注明:转自《PHP获取百度收录数量及百度快照

分享本文: 腾讯微博 QQ空间 人人网 百度空间 开心网 新浪微博 Google Reader 豆瓣
Comments(30) Leave comments
  1. Gravatar
    IT路人 Google Chrome Google Chrome 15.0.874.106 Windows Windows 7

    功能使用,有空鼓捣一下

  2. Gravatar
    deardongdong Google Chrome Google Chrome 13.0.782.220 Windows Windows XP

    这个比较牛啊。但我通常都是在别的网站查呢。

  3. Gravatar
    黄健祥 Internet Explorer Internet Explorer 8.0 Windows Windows XP

    这个都研究 太给力了哦
    你为什么取名 星期九 我很好奇哦

    • Gravatar Harid  @  八月 26th, 2011 at 19:26 replied.

      @黄健祥, 为什么叫“星期九”在我的关于页面有说明吧

  4. Gravatar
    栲栳岛旅游 Mozilla Firefox Mozilla Firefox 5.0 Windows Windows XP

    真专业。。。

  5. Gravatar
    煎豆 Google Chrome Google Chrome 13.0.782.107 Windows Windows 7

    怎么申请那个备案的啊??

    • Gravatar Harid  @  八月 7th, 2011 at 20:26 replied.

      @煎豆, 什么备案?ICP备案吗?你不是有备案号吗?

      • Gravatar 煎豆  @  八月 7th, 2011 at 23:06 replied.  | #6

        @Harid, 还没办下来,那个是虚设的。你是蓝冰帮你办的么??

        • Gravatar Harid  @  八月 7th, 2011 at 23:35 replied.

          @煎豆, 还没办下来也可以放了?!我的备案是在域名刚注册就备案好了的,那个时候备案还很容易的,一年之前了。现在的备案我也备过,我是等了近三个星期才下来的,中间还不能打电话给管局询问,只能干等,而且各地的通信局办事效率很不一样,通过率也很不一样。我备案了好几次了,都是在湖南省通信局备的,貌似湖南的通过比较容易。

      • Gravatar 煎豆  @  八月 7th, 2011 at 23:07 replied.  | #7

        @Harid,邮寄开始 花了好久时间啊??

  6. Gravatar
    煎豆 Google Chrome Google Chrome 13.0.782.107 Windows Windows 7

    我们在同一主机上~~~ :grin:

    • Gravatar Harid  @  八月 7th, 2011 at 20:24 replied.

      @煎豆, 那就有点生死与共的味道了,一挂同挂, :lol:

评论分页
6 + 8 =  (required)
 疑问 鼓掌 难过 呲牙 强 微笑 快哭了 坏笑 汗 奋斗 撇嘴 OK 偷笑 委屈 尴尬 傲慢 握手 玫瑰 胜利 大哭 抱拳
启用云输入法:      

NOTICE1: You should type some Chinese word (like “你好”) in your comment to pass the spam-check, thanks for your patience!

NOTICE2: 请申请gravatar头像(http://en.gravatar.com),木有头像的会显示为“小怪物”头像,将难以通过审核!

NOTICE3: 如果您能消除一下评论框旁边的邻居的寂寞的话,Harid将不胜感激,你懂的!^_^