【问题标题】:How to get more that 10 results for a query using YQL?如何使用 YQL 获得超过 10 个查询结果?
【发布时间】:2011-06-07 18:30:32
【问题描述】:

我正在尝试使用YQL 在 php 中使用 amazon.prodlist 表和亚马逊产品广告 API 从亚马逊获取产品信息。

我使用的查询:

select * from amazon.prodlist where Title='harry potter' and SearchIndex='Books' and ResponseGroup='Images,ItemAttributes'

它只返回 10 个结果。我怎样才能让它在同一页面上显示超过 10 个结果?此外,没有分页。

完整的 PHP 代码:

<?php
$BASE_URL = "https://query.yahooapis.com/v1/public/yql";

$key="my_key";
$secret="my_secret";
$title="harry potter";
$sindex="Books";
$rgroup="Images,ItemAttributes";
$events="";
     

// Form YQL query and build URI to YQL Web service
$yql_query = "use 'http://www.datatables.org/amazon/amazon.ecs.xml' as amazon.prodlist;
set AWSAccessKeyId='$key' on amazon.prodlist;
set secret='$secret' on amazon.prodlist; 
select * from amazon.prodlist where Title='$title' and SearchIndex='$sindex' and ResponseGroup='$rgroup' ";

$yql_query_url = $BASE_URL . "?q=" . urlencode($yql_query) . "&format=json";

// Make call with cURL
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
// Convert JSON to PHP object 
$phpObj =  json_decode($json);

// Confirm that results were returned before parsing
if(!is_null($phpObj->query->results)){
  // Parse results and extract data to display
  foreach($phpObj->query->results->Item as $event){
    $events .= "<div><h2>" . $event->ItemAttributes->Title . " by " . $event->ItemAttributes->Author . "</h2></div>";
}
}
// No results were returned
if(empty($events)){
  $events = "Sorry, no events matching result";
}
// Display results and unset the global array $_GET
echo $events;
unset($_GET);

?>

这会在一个页面上显示 10 个结果。然而,当我在亚马逊网站上的“书籍”中搜索“哈利波特”时,我得到了超过 3k 条结果。有没有办法在一个页面上获得所有结果?请指教。

【问题讨论】:

  • 或许读过the documentation?
  • 我已经阅读并尝试将查询更改为:select * from amazon.prodlist(20) where Title='$title' and SearchIndex='$sindex' and ResponseGroup='$rgroup'、select * from amazon.prodlist(0) where Title='$title' and SearchIndex='$sindex' and ResponseGroup='$rgroup'、select * from amazon.prodlist(0) where Title='$title' and SearchIndex='$sindex' and ResponseGroup='$rgroup' limit 20 但我仍然只得到 10 个结果。还有其他建议吗?
  • 好的,对 Amazon 或 YQL 都没有太多经验,所以我会留给其他人,不过下次你可能会提到这个小事实,可以节省人们的时间 :)

标签: php amazon-web-services yql amazon-product-api


【解决方案1】:

amazon.ecs 开放数据表(在撰写您的问题时)不支持结果分页,因此您只能检索 10 个项目。这是开放数据表作者的常见疏忽。

我已在自己的 YQL 表存储库分支中修改了数据表源,并发出了拉取请求 (here),希望将更改恢复到主要源中。然后,您将能够使用 table.name([offset,]count) 语法 (docs) 获得更多(或更少!)结果。

如果您想立即启动并运行,那么您需要更改数据表的 URL 以指向我在一个特殊分支中的更改,只是针对这个问题:

https://github.com/salathe/yql-tables/blob/so-6269923/amazon/amazon.ecs.xml

您的完整查询将如下所示(非常与您现有的查询相似):

use 'https://raw.github.com/salathe/yql-tables/so-6269923/amazon/amazon.ecs.xml' as amazon.prodlist;
use AWSAccessKeyId='my_access_key' on amazon.prodlist;
use secret='my_secret' on amazon.prodlist; 

select *
from amazon.prodlist(50) 
where Title='harry potter' 
  and SearchIndex='Books' 
  and ResponseGroup='Images,ItemAttributes';

当(如果...密切关注 pull request)更改被拉回主 YQL 表存储库时,您将能够重新使用 http://www.datatables.org/amazon/amazon.ecs.xml URL。

【讨论】:

  • 非常感谢!我在yql控制台试了下查询,运行流畅!当我在上面的 php 代码中使用相同的查询时遇到一些错误,但希望我能解决它们。赞!
  • 好消息,更改已拉入主 YQL 表。您可以返回使用http://www.datatables.org/amazon/amazon.ecs.xml。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多