【发布时间】:2020-12-01 19:18:06
【问题描述】:
我已尝试使用以下代码从 google 搜索结果中获取所有 URL 的列表。但我能够找到带有描述的 URL。我只想获取 URL 作为列表。
<?php
include('simple_html_dom.php');
function file_get_contents_curl($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //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 = "what is a car";
$url = 'http://www.google.com/search?q=' . urlencode($query) . '';
$scrape = file_get_contents_curl($url);
//gettype($scrape);
//echo $scrape;
$domResults = new simple_html_dom();
$domResults->load($scrape);
foreach ($domResults->find('a[href^=/url?q]') as $element) {
echo $element . '<br><br>';
}
?>
【问题讨论】:
标签: php parsing dom google-search