【发布时间】:2015-08-03 19:50:17
【问题描述】:
我正在使用 php 从谷歌搜索结果中提取数据。 这是我在 php 中的代码。
<?php
$url="https://www.google.co.in/search?
q=allintext:Theatre%20Actors&sort=date&cr=countryIN&aqs=chrome..69i57.1033j0j7&sourceid=chrome&es_sm=93";
$homepage = file_get_contents($url);
$ans = htmlentities($homepage);
$doc = new DOMDocument();
@$doc->loadHTML($homepage);
$tags = $doc->getElementsByTagName('h3');
$finder = new DOMXPath($doc);
$node = $finder->query("//h3[contains(@class, 'r')]");
foreach ($node as $tag) {
$temp = $tag->getElementsByTagName('a');
$string = "";
foreach ($temp as $key) {
$k=0;
$data = $key->getAttribute('href');
//echo $data;
for($i=0;$i<strlen($data);$i++){
if($data[$i]=="&") break;
if($data[$i]=="="){
$k=1; continue;
}
if($k==1){
$string.=$data[$i];
}
}
}
$idx= split(":", $string);
if(strcmp($idx[0],"http")!=0 && strcmp($idx[0],'https')!=0) continue;
$ans = '<a href='.$string.'>'.$string.'</a>';
echo $ans;
echo '<hr>';
}
?>
运行此脚本后,我收到此错误: 警告:file_get_contents(https://www.google.co.in/search?q=allintext:Theatre%20Actors&sort=date&cr=countryIN&aqs=chrome..69i57.1033j0j7&sourceid=chrome&es_sm=93):打开流失败:HTTP 请求失败! HTTP/1.0 503 Service Unavailable in /opt/lampp/htdocs/search/script.php on line 4
请帮忙。 提前致谢。
【问题讨论】:
标签: php