在 Google 购物上使用 Google 自定义搜索

Dej*_*ell 6 google-api-java-client google-custom-search

我想进行 API 调用以检索 Google 购物结果(基本上是产品的价格)我登录到Google 自定义搜索,创建了一个名为 General 的新搜索引擎,并在网站中选择:http : //www.google。 com/购物

但是,当我尝试搜索时,我只得到 1 个结果并且没有价格。

如何检索包括商品价格在内的 Google 购物结果?有没有其他方法而不是报废页面?(我认为完全不推荐)

Uts*_*awn 3

您可以从 Google Content API 获取所有产品详细信息以进行购物,包括产品的价格。以下是用于检索单个产品详细信息的代码片段:

  /**
* Retrieves the product with the specified product ID from the Content API for Shopping
Server.
*
* @param productId The ID of the product to be retrieved.
* @return The requested product.
* @throws HttpResponseException if the retrieval was not successful.
* @throws IOException if anything went wrong during the retrieval.
*/

private Product getProduct(String productId)
  throws IOException, HttpResponseException {
    // URL
    String url = rootUrl + userId + "/items/products/schema/" + productId;

    // HTTP request
    HttpRequest request = requestFactory.buildGetRequest(new GoogleUrl(url));

    // execute and interpret result
    return request.execute().parseAs(Product.class);
}
Run Code Online (Sandbox Code Playgroud)

您需要为上述代码编写一个 Product 模型类。该类看起来像:

/**
* Class for representing a product entry.
*/
public class Product extends BatchableEntry {
    @Key("sc:additional_image_link")
    public List<String> additionalImageLinks;
    ...
    @Key("scp:price")
    public Price price;
    ...
    @Key("scp:product_type")
    public String productType;

    @Key("scp:quantity")
    public Integer quantity;
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

您可以下载完整的源代码并从Google Developers 提供的此示例中获取代码的解释。