搜索亚马逊只返回10个项目

时间:2010-04-06 06:11:29

标签: amazon amazon-product-api

我正在尝试从亚马逊获取有关图书的信息并提供该信息。到我自己的网络应用程序。问题是它只返回了10个结果。如何在前10个后获得结果?

1 个答案:

答案 0 :(得分:3)

我假设您正在使用Amazon Product Advertising API中的ItemSearch操作。

您的请求应如下所示:

http://ecs.amazonaws.com/onca/xml?
Service=AWSECommerceService&
AWSAccessKeyId=[AWS Access Key ID]&
Operation=ItemSearch&
Keywords=Edward%20Tufte&
SearchIndex=Books
&Timestamp=[YYYY-MM-DDThh:mm:ssZ]
&Signature=[Request Signature]

这应该返回一个看起来像的响应:

<TotalResults>132</TotalResults>
<TotalPages>14</TotalPages>
<Item>
  <ASIN>...</ASIN>
  <DetailPageURL>...</DetailPageURL>
  <ItemAttributes>...</ItemAttributes>
</Item>
<Item>
  <ASIN>...</ASIN>
  <DetailPageURL>...</DetailPageURL>
  <ItemAttributes>...</ItemAttributes>
</Item>
<Item>
  <ASIN>...</ASIN>
  <DetailPageURL>...</DetailPageURL>
  <ItemAttributes>...</ItemAttributes>
</Item>
...

ItemSearch结果是分页的;上述请求将返回项目1到10(对应于第1页)。要获得其他结果,您需要请求不同的结果页面。使用Amazon ItemSearch操作,您可以通过指定itemPage参数来执行此操作。

以下是sudo代码,可以通过亚马逊上提供的“Edward Tufte”获取所有书籍(最多400页结果):

keywords="Edward Tufte"

# itemSearch will create the Amazon Product Advertising request
response=itemSearch(Keywords=keywords, SearchIndex="Books")
# Do whatever you want with the response for the first page
...

# getTotalPagesFromResponse will parse the XML response and return the totalPages
# (14 in the above example). 
totalPages = getTotalPagesFromResponse(response)
If totalPages > 1
  # Note that you cannot go beyond 400 pages (see [1])
  # Or you can limit yourself to a smaller number of pages
  totalPages=min(400,totalPages)

  page=2
  while page < totalPages
    response=itemSearch(Keywords=keywords, SearchIndex="Books", ItemPage=page)
    # Do whatever you want with the response
    ...
    page=page+1

参考: [1] ItemSearch亚马逊产品文档(可在http://docs.amazonwebservices.com/AWSECommerceService/2010-11-01/DG/ItemSearch.html获得)