在谷歌搜索结果中只捕捉网站链接


Catch only websites links in Google search Results

我有以下代码:

$urls = file_get_contents('https://www.google.com/#q=test');
preg_match_all('/'b(?:(?:https?|http):'/'/|www'.)[-a-z]*.com/i', $urls, $content);
$i = 10;
while ( $i <= 50 ) {
$i+= 10;
$urls2 = file_get_contents('https://www.google.com/#q=test&start=".$i."'); // pagination Google search Results
preg_match_all('/'b(?:(?:https?|http):'/'/|www'.)[-a-z]*.com/i', $urls2, $contentLoop);
$totalArray = array_push($content,$contentLoop);

}
print_r($totalArray);

只打印数字6

在while中,我如何在单个数组中添加多个数组?

我尝试使用函数array_push,但是到目前为止没有成功

Array_push仅用于将一个元素压入数组末尾。这里可以使用两种可能的解决方案之一(两者都将数据保存到$content数组中):

  1. array_merge使用。

    array_merge($content,$contentLoop);
    
  2. 通过$contentLoop循环。

    foreach($contentLoop as $item){
        array_push($content,$item);
    }
    

如果您想将两个arrays合并为一个,那么您可以使用array_merge -合并一个或多个数组。

<?php
$totalArray = array_merge($content,$contentLoop);
print_r($totalArray);
?>

查看官方文档:

array_merge -合并一个或多个数组

你似乎在试图抓取谷歌搜索结果。抓取违反了谷歌的服务条款。谷歌有一个网络搜索api,但在2014年停止了。Google现在提供了自定义搜索api。既然Google web搜索API已经被弃用了,那么还有什么替代方案呢?