search
Homephp教程PHP源码抓取百度排名

抓取百度排名

May 25, 2016 pm 05:13 PM
php

抓取百度排名      

1. [图片] so.png    

抓取百度排名

2. [代码][PHP]代码 

function s($keyword,$url,$page = 1){
	static $px = 0;
	$rsState = false;
	
	$enKeyword = urlencode($keyword);
	$firstRow = ($page - 1) * 10;
	
	if($page > 10){
		die('10页之内没有该网站排名..end');
	}	
	$contents = file_get_contents("http://www.baidu.com/s?wd=$enKeyword&&pn=$firstRow");
	preg_match_all(&#39;/<table[^>]*?class="result"[^>]*>[\s\S]*?<\/table>/i&#39;,$contents,$rs);
	foreach($rs[0] as $k=>$v){
		$px++;
		if(strstr($v,$url)){
			$rsState = true;
			preg_match_all(&#39;/<h3[\s\S]*?(<a[\s\S]*?<\/a>)/&#39;,$v,$rs_t);
			echo &#39;当前 "&#39; . $url . &#39;" 在百度关键字 "&#39; . $keyword . &#39;" 中的排名为:&#39; . $px;
			echo &#39;<br>&#39;;
			echo &#39;第&#39; . $page . &#39;页;第&#39; . ++$k . "个<a target=&#39;_blank&#39; href=&#39;http://www.baidu.com/s?wd=$enKeyword&&pn=$firstRow&#39;>进入百度</a>"; 
			echo &#39;<br>&#39;;
			echo $rs_t[1][0];
			break;
		}
	}
	unset($contents);
	if($rsState === false){
		s($keyword, $url,++$page);
	}
}
if(isset($_POST[&#39;submit&#39;])){
	
	$time = explode(&#39; &#39;,microtime());
	$start = $time[0] + $time[1];

	$url = $_POST[&#39;url&#39;];
	if( count(explode(&#39;.&#39;,$url)) <= 2){

		$url = ltrim($url,&#39;http://&#39;);
		$url = &#39;www.&#39; . $url;
	}

	s($_POST[&#39;keyword&#39;],$url);
	
	$endtime = explode(&#39; &#39;,microtime());

	$end = $endtime[0] + $endtime[1];

	echo &#39;<hr>&#39;;
	echo &#39;程序运行时间: &#39;;
	echo $end - $start;	
	die();
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>抓取排名</title>

</head>

<body>
<form action="" method="post">
	<ul>
		<li>
			<span>关键字:</span><input type="text" name="keyword">
		</li>
		<li>
			<span>url地址:</span><input type="text" name="url">
		</li>
		<li>
			<input type="submit" name="submit" value="搜索">
		</li>
	</ul>
	
</form>
</body>
</html>


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools