使用SerpApi.com在规模上抓取并解析Google、Bing、Baidu、Ebay、Youtube、Walmart、Yandex的搜索结果

2.0.4 2022-05-19 09:57 UTC

This package is not auto-updated.

Last update: 2024-09-20 20:48:50 UTC


README

PHP build

此Php API旨在使用SerpApi(https://serpapi.com)抓取并解析Google、Bing或Baidu的结果。

完整的文档在此处可用。

以下服务提供

SerpApi提供了一个脚本构建器,让您快速入门。

安装

必须已安装Php 7+和composer依赖关系管理工具。

包可在packagist上获取。

快速开始

如果您使用composer,可以添加此包(packagist链接)。

$ composer require serpapi/google-search-results-php

然后您需要在您的脚本中加载此依赖。

<?php
require __DIR__ . '/vendor/autoload.php';
 ?>

如果不这样做,您必须克隆此存储库并链接类。

require 'path/to/google-search-results';
require 'path/to/restclient';

https://serpapi.com/dashboard获取“您的密钥”。

然后您可以开始编写类似以下内容

$client = new GoogleSearch("your secret key");
$query = ["q" => "coffee","location"=>"Austin,Texas"];
$response = $client->get_json($query);
print_r($json_results)

此示例使用您的密钥运行有关“咖啡”的搜索。

SerpApi服务(后端)

  • 使用查询:q = "coffee" 在Google上搜索
  • 解析混乱的HTML响应
  • 返回标准化的JSON响应 Php类GoogleSearch
  • 将请求格式化为发送给SerpApi服务器的格式
  • 执行GET http请求
  • 使用Ruby标准库将JSON解析为Ruby哈希 Et voila..

或者,您可以

  • 使用BingSearch类使用Bing搜索
  • 使用BaiduSearch类使用Baidu搜索
  • 使用EbaySearch类使用Ebay搜索
  • 使用YahooSearch类使用Yahoo搜索
  • 使用YandexSearch类使用Yandex搜索
  • 使用WalmartSearch类使用Walmart搜索
  • 使用YoutubeSearch类使用Youtube搜索

查看游乐场以生成您的代码。 https://serpapi.com/playground

示例

如何设置SERP API密钥

可以使用单例模式全局设置SerpApi api_key。

$client = new GoogleSearch();
$client->set_serp_api_key("Your Private Key");

或者

$client = new GoogleSearch("Your Private Key");

搜索API功能

$query = [
  "q" =>  "query",
  "google_domain" =>  "Google Domain", 
  "location" =>  "Location Requested", 
  "device" =>  "device",
  "hl" =>  "Google UI Language",
  "gl" =>  "Google Country",
  "safe" =>  "Safe Search Flag",
  "num" =>  "Number of Results",
  "start" =>  "Pagination Offset",
  "serp_api_key" =>  "Your SERP API Key",
  "tbm" => "nws|isch|shop"
  "tbs" => "custom to be search criteria"
  "async" => true|false # allow async 
];

$client = new GoogleSearch("private key");

$html_results = $client->get_html($query);
$json_results = $client->get_json($query);

位置API

$client = new GoogleSearch($this->API_KEY);
$location_list = $client->get_location('Austin', 3);
print_r($location_list);

它打印出与Austin(德克萨斯州,德克萨斯州,罗切斯特)匹配的前3个位置

[{:id=>"585069bdee19ad271e9bc072",
  :google_id=>200635,
  :google_parent_id=>21176,
  :name=>"Austin, TX",
  :canonical_name=>"Austin,TX,Texas,United States",
  :country_code=>"US",
  :target_type=>"DMA Region",
  :reach=>5560000,
  :gps=>[-97.7430608, 30.267153],
  :keys=>["austin", "tx", "texas", "united", "states"]},
  ...]

搜索存档API

让我们运行一个搜索以获取搜索_id。

$client = new GoogleSearch($this->API_KEY);
$result = $client->get_json($this->QUERY);
$search_id = $result->search_metadata->id

现在让我们从存档中检索之前的搜索。

$archived_result = $client->get_search_archive($search_id);
print_r($archived_result);

它打印出存档中的搜索。

账户API

$client = new GoogleSearch($this->API_KEY);
$info = $client->get_account();
print_r($info);

它打印出您的账户信息。

搜索Google图片

$client = new GoogleSearch($this->API_KEY);
$data = $client->get_json([
  'q' => "Coffee", 
  'tbm' => 'isch'
]);

foreach($data->images_results as $image_result) {
  print_r($image_result->original);
  //to download the image:
  // `wget #{image_result[:original]}`
}

此代码打印出所有图像链接,并且如果取消注释带有wget的行(Linux/osx下载图像的工具),则下载图像。

根据规范示例

上述代码已在test.php和example.php文件中测试。要本地运行测试。

export API_KEY='your secret key'
make test example

Composer示例

查看: https://github.com/serpapi/google-search-results-php/example_composer/

要运行代码。

变更日志

  • 2.0
    • 代码重构 SearchResult -> Search
    • 添加walmart和youtube搜索引擎
  • 1.2.0
    • 添加更多搜索引擎
  • 1.0
    • 第一个稳定版本

      结论

serpapi支持所有主要的搜索引擎。Google提供了更高级的支持,包括所有主要服务:图片、新闻、购物等。要启用一种搜索类型,必须将tbm(待匹配)字段设置为

  • isch:Google图片API。
  • nws:Google新闻API。
  • shop:Google购物API。
  • 其他任何Google服务都应该直接可用。
  • (没有tbm参数):常规Google搜索。字段tbs允许进一步定制搜索。

完整的文档在此处可用。

作者:Victor Benarbia victor@serpapi.com 更多信息:https://serpapi.com

感谢PHP Rest API