2013-02-20 2 views
2

나는 현재 Google에서 키워드 제안을 긁어 모으고 있습니다. 나는 그것을 테스트 할 때 어떤 이유로,긁어 모으기 키워드 제안 구글에서

["money",["moneygram","money network","money mutual","money trees lyrics","moneyball","moneypak","money","money converter","money order","money2india"]] 

그러나 다음 URL을 통해 직접 액세스 할 때

<?php 
function text_between($start,$end,$string) { 
    if ($start != '') {$temp = explode($start,$string,2);} else {$temp = array('',$string);} 
    $temp = explode($end,$temp[1],2); 
    return $temp[0]; 
} 
function gsscrape($keyword) { 
    $keyword=str_replace(" ","+",$keyword); 
    global $kw; 
    $data=file_get_contents('http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q='.$keyword); 
    $data=explode('[',$data,3); 
    $data=explode('],[',$data[2]); 
    foreach($data as $temp) { 
    $kw[]= text_between('"','"',$temp); 
    } 
} 
#simple to use, just use yourscriptname.php?keywords 

if ($_SERVER['QUERY_STRING']!='') { 
    gsscrape($_SERVER['QUERY_STRING']); 
    foreach ($kw as $keyword) { 
    gsscrape($keyword); 
    } 

//sorted and duplicates removed 
sort(array_unique($kw)); 

#all results echoed with break 
foreach ($kw as $keywords) { 
echo $keywords. "<br />"; 
} 

} 
?> 

구글이 나에게 키워드 money이 응답을 줄 것이다 : 이것은 내가 함께 일하고 있어요 스크립트입니다 내 웹 사이트에서 다음과 같이 표시됩니다.

moneygram 
moneygram 

이렇게 각 키워드가 표시되도록 변경해야 할 사항은 무엇입니까?

moneygram, money network, money mutual, money trees lyrics, moneyball, moneypak, money, money converter, money order, money2india 
+1

당신은 http://scrape-google-suggest.compunect.com/ PHP 구글이 제안하는 오픈 소스입니다 을 확인해야합니다 : 배열이 사용으로

<?php function getKeywordSuggestionsFromGoogle($keyword) { $keywords = array(); $data = file_get_contents('http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q='.urlencode($keyword)); if (($data = json_decode($data, true)) !== null) { $keywords = $data[1]; } return $keywords; } var_dump(getKeywordSuggestionsFromGoogle('money')); 
John

답변

8

유효한 JSON입니다. 을 사용하면 완료되었습니다.

var_dump(json_decode('["money",["moneygram","money network","money mutual","money trees lyrics","moneyball","moneypak","money","money converter","money order","money2india"]]')); 

편집 - 전체 예;

function gsscrape($keyword) { 
     return json_decode(utf8_decode(file_get_contents('http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q='.urlencode($keyword))),true); 
    } 
+0

현재 스크립트에서 어떻게 사용합니까? 결과는 키워드에 따라 달라질 것입니다 ... – user2090227

+0

글쎄, 당연히 .. 당신은 당신이 구글로부터 돌아와서 당신의 json_decode에 대한 입력으로 가지고있는 데이터를 보낼 것입니다. 이것은 정말로 간단한 것이지만, 완전한 예를 들어 .. – hank

+0

그 놀라운 것입니다! 문제가 생겨서 죄송합니다. 처음에는이 물건을 찾아 내려고했습니다. :) – user2090227

2

데이터를 얻으려면 스크래핑 거미 당신이 정확히 무엇을 프로그램하고 싶은 일을 정확하게하고 (로컬 캐싱, resursive 스파이더, ip 관리) 당신이 코드에 충실하더라도, 내가 링크 된 하나는 유용하게 찾을 수있는 기능을 포함 할 수 있습니다.
관련 문제