此脚本在google.com上运行正常但与google.com/search?q=test无效。当我不使用CURLOPT_FOLLOWLOCATION时,我得到一个302 Moved。当我使用它时,我得到一个页面,要求我输入验证码。我尝试了几种不同的美国代理,并改变了用户代理字符串。这里有什么我想念的吗?
function my_fetch($url,$proxy,$user_agent='Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8')
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_PROXY, $proxy);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec ($ch);
curl_close ($ch);
return $result;
}
$url = 'http://www.google.com/search?q=test';
$proxy = '152.26.53.4:80';
echo my_fetch($url,$proxy);
请不要回复使用API的建议。 API不足以满足我的需求。
答案 0 :(得分:0)
Google不再使用cURL。
Google不再通过Curl提供访问权限,它可能会为您提供302 Moved消息,如果您想使用它,则必须使用API。
谢谢
答案 1 :(得分:0)
您可以尝试使用PhantomJS:
var page = require("webpage").create();
var homePage = "http://www.google.com/";
page.open(homePage);
page.onLoadFinished = function(status) {
var url = page.url;
console.log("Status: " + status);
console.log("Loaded: " + url);
page.includeJs("http://code.jquery.com/jquery-1.8.3.min.js", function() {
console.log("Loaded jQuery!");
page.evaluate(function() {
var searchBox = $(".lst");
var searchForm = $("form");
searchBox.val("your query");
searchForm.submit();
});
});
window.setTimeout(
function () {
page.render( 'google.png' );
phantom.exit(0);
},
1000 // wait 5,000ms (5s)
);
};