3

Amazon から書籍に関する情報を取得し、その情報をフィードしようとしています。自分の Web アプリに。問題は、10 件の結果しか返されなかったことです。最初の 10 回の結果を取得するにはどうすればよいですか?

4

1 に答える 1

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 パラメータを指定してこれを行います。

Amazon で入手可能な「Edward Tufte」による、または「Edward Tufte」に関するすべての本を取得する sudo コードを次に示します (最大 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 Amazon 製品ドキュメント ( http://docs.amazonwebservices.com/AWSECommerceService/2010-11-01/DG/ItemSearch.htmlで入手可能)

于 2011-04-22T23:15:44.547 に答える