- <html>
- <head>
- <title>在线演示_php获取百度关键词查询结果总数</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <meta name="keywords" content="百度,baidu,查询结果">
- <meta name="description" content="php获取百度关键词查询结果总数">
- </head>
- <body>
- <?php $k=isset($_POST['k'])?$_POST['k']:''; ?>
- <form method="post" action="">
- 查询关键词:
- <input type="text" name="k" size="50" value="<?php echo $k ?>">
- <input type="submit" value="提交">
- </form>
- <?php
- /*
- 获取百度关键词查询结果总数
- 2012-7-26
- @param str $keyword
- @param str 整型字符
- */
- function getKeywordTotal($keyword) {
- $url = 'http://www.baidu.com/s?wd='.$keyword;
- $html = file_get_contents($url);
- $search = '/<span class="nums"[^>]*?>[^<]*?([0-9,]*?)</i'; //获取<span class="nums">中部分
- preg_match($search,$html,$match);
- $match = str_replace(',','',$match); //去逗号
- preg_match('/<span[^>]*>.*?(\\d+)/',$match[0],$r);//得到最后的总数:
- return $r[1];
- }
- if($k=='') {
- exit('请输入数据');
- }else{
- echo '共有'.getKeywordTotal($k).'个结果。';
- }
- ?>
- </body>
- </html>
- //该片段来自于http://www.codesnippet.cn/detail/170620134079.html
来源: http://www.codesnippet.cn/detail/170620134079.html