【发布时间】:2021-08-12 02:30:43
【问题描述】:
我想返回 google 搜索结果的第一个 URL,例如: First Url Result
.php代码:
<?php
//Check if submit button is clicked or not
if (isset($_POST['submit'])) {
$text = $_POST['text'];
// echo $text;
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$query = $text;
$url = 'http://www.google.co.in/search?q='.urlencode($query).'';
echo $url;
$scrape = file_get_contents_curl($url);
echo $scrape;
?>
我怎样才能做到这一点?
【问题讨论】:
-
你没有说上面代码的结果是什么,但谷歌通常不喜欢你试图抓取他们的搜索页面,你可能会发现它在某些情况下被阻止/混淆/不可用来自 cURL 请求的方式。改用他们的搜索 API。
标签: php google-search