【发布时间】:2019-12-09 05:55:53
【问题描述】:
最近,我想知道用 PHP 编写的网络爬虫是否可以立即重定向到在 google 搜索中获取的第一个 url。
<?php
include('simple_html_dom.php');
$html = file_get_html('https://www.google.com/search?q=raspberry&oq=raspberry&aqs&num=1');
$linkObjs = $html->find('div[class=jfp3ef] a');
foreach ($linkObjs as $linkObj) {
$title = trim($linkObj->plaintext);
$link = trim($linkObj->href);
//if it is not a direct link but url reference found inside it, then extract
if (!preg_match('/^https?/', $link) && preg_match('/q=(.+)&sa=/U', $link, $matches) && preg_match('/^https?/', $matches[1])) {
$link = $matches[1];
} else if (!preg_match('/^https?/', $link)) { // skip if it is not a valid link
continue;
}
echo $link . '</p>';
}
?>
该代码从谷歌搜索“raspberry”中获取第一个顶级结果并打印该网站的网址。我希望它把它重定向到那个 url 而不是打印出来。
【问题讨论】:
标签: php html dom web-scraping