구글 검색결과 개수 얻기 (PHP)

구글 검색결과 개수 얻기
PHP로 구글 검색결과 수 확인
구글링 개수
googling_count

1 소스코드[ | ]

<?php
function googling_count($search) { 
	$search = urlencode($search); 
	$url = "http://www.google.co.in/search?q=$search&btnG=Search&meta="; 
	$contents = file_get_contents($url);

	$s_str = 'id="resultStats">About';
	$e_str = 'results';
	$result = get_between($s_str, $e_str, $contents);
	$result = trim($result);
	$result = str_replace(',', '', $result);
	$left = substr($result,0,2);
	if($left == '0b') return bindec($result);
	if($left == '0o') return octdec($result);
	if($left == '0x') return hexdec($result);
	return $result;
}

function get_between($start, $end, $str) {
	$pos1 = strpos($str, $start);
	if($pos1 === false) return '';
	$pos1 += strlen($start);
	$pos2 = strpos($str, $end, $pos1);
	if($pos2 === false) return '';
	return substr($str, $pos1, $pos2 - $pos1);
}

$keywords = array('school', 'hello world', 'binary', 'octal', 'hexadecimal');
foreach($keywords as $keyword) {
	$count = googling_count($keyword);
	echo "[$keyword]: $count<br>";
}

2 실행결과[ | ]

[school]: 4150000000
[hello world]: 568000000
[binary]: 173000000
[octal]: 68500000
[hexadecimal]: 80400000

3 트래픽 제한[ | ]

과도한 트래픽을 방지하기 위해 블랙리스트 관리가 되는 모양이다. 정확한 수치는 확인하지 못했지만, 한 시간에 약 1000개 이상을 검색했더니 503 Error가 나왔다.

Warning: file_get_contents(http://www.google.com/search?q=검색어) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable

제한은 한 시간 정도 후에 해제된다.

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}