Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

php - Failing to retrieve a page with file_get_contents

I want to scrape some google page with file get contents function:

     $encoded='http://www.google.co.il/#hl=en&biw=1440&bih=799&sclient=psy-ab&q=site:'.urlencode("http://stackoverflow.com/");

    echo  file_get_contents($encoded);

When I do this:

echo $encoded;

I get this:

http://www.google.co.il/#hl=en&biw=1440&bih=799&sclient=psy-ab&q=site:http%3A%2F%2Fstackoverflow.com%2F

When I put it in the url.. I get the page that I want

But when I use the function , I simply get the main google page..why does this happen?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's because you url uses googles new client side side functionality. Everything after the # is for the client, and the server will serve you the content under http://www.google.co.il/.

update

If you want to use google search results in your program, try using one of their API for custom search. There have been a search webservice for ajax clients under the this url:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=your+term+goes+here

It returns results in JSON, the first result is under responseData->results array:

$search_results = json_decode(file_get_contents('http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=your+term+goes+here'), true);
var_export($search_results['responseData']['results'][0]);

But i think this have been deprecated since than (still responds however).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...