howie6879/php-google

此包最新版本(1.0)没有可用的许可证信息。

谷歌搜索结果爬虫,获取您需要的谷歌搜索结果。

1.0 2017-06-24 07:18 UTC

This package is not auto-updated.

Last update: 2024-09-24 17:14:03 UTC


README

这是一个简单的谷歌搜索爬虫,您可以使用它来获取页面上的任何内容。

在爬取过程中,您需要关注谷歌对IP地址的限制以及异常警告,因此我建议您暂停程序运行并获取代理IP。

python - MagicGoogle

2. 如何使用?

您可以通过在composer.json中要求howie6879/php-google包来通过composer安装此项目。

{
    "require": {
        "howie6879/php-google": "1.0"
    }
}

如果您已在项目中安装了php-google,您可以得到所需的谷歌搜索结果。

示例

# Add boostrap autoload file

require_once '../vendor/autoload.php';
use \howie6879\PhpGoogle\MagicGoogle;

# Or new MagicGoogle()
$magicGoogle = new MagicGoogle('http://127.0.0.1:8118');

# The first page of results
$data = $magicGoogle->search_page('python');

# Get url
$data = $magicGoogle->search_url('python');

foreach ($data as $value) {
    var_dump($value);
}

/** Output
 * string(23) "https://pythonlang.cn/"
 * string(33) "https://pythonlang.cn/downloads/"
 * string(35) "https://docs.pythonlang.cn/3/tutorial/"
 * string(44) "https://pythonlang.cn/about/gettingstarted/"
 * string(43) "https://wiki.python.org/moin/BeginnersGuide"
 * string(41) "https://pythonlang.cn/downloads/windows/"
 * string(24) "https://docs.pythonlang.cn/"
 * string(59) "https://en.wikipedia.org/wiki/Python_(programming_language)"
 * string(39) "https://www.codecademy.com/learn/python"
 * string(25) "https://github.com/python"
 * string(38) "https://tutorialspoint.org.cn/python/"
 * string(28) "https://www.learnpython.org/"
 * string(44) "https://www.programiz.com/python-programming"
 */
 
# Get {'title','url','text'}
$data = $magicGoogle->search('python', 'en', '1');

foreach ($data as $value) {
    var_dump($value);
}

/** Output
 * array(3) {
 * ["title"]=>
 * string(21) "Welcome to Python.org"
 * ["url"]=>
 * string(23) "https://pythonlang.cn/"
 * ["text"]=>
 * string(54) "The official home of the Python Programming Language. "
 * }
 */

您可以看到 sample.php

如果您需要大量查询但只有一个IP地址,我建议您在5秒到30秒之间设置时间间隔。

它总是返回空的原因可能如下

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://ipv4.google.com/sorry/index?continue=https://www.google.me/s****">here</A>.
</BODY></HTML>