我有一个网站(www.7bks.com),人们可以在其中创建书单。目前它相当简单。我已经在使用 Amazon API 将书籍信息、图像等拉到网站上。
我想做的是以某种方式使用亚马逊 API 来拉回类别和/或标签数据,以在我的网站上创建某种浏览列表的方式。不幸的是,tag api 方法已停止使用。
最有可能的候选者是 Amazon API 的 BrowseNodes 方法(http://docs.amazonwebservices.com/AWSEcommerceService/2005-10-05/ApiReference/BrowseNodesResponseGroup.html),但是从这个调用返回的数据非常荒谬,我是希望我们能够齐心协力,弄清楚如何理解它。
这是一个谷歌电子表格,向您展示我得到的数据类型。我选择了一个示例列表(http://www.7bks.com/list/549002)并通过 BrowseNodes API 运行了这三本书:
以人类的身份查看列表,您无需知道这些书是什么,就可以看出该列表很可能是关于科幻和幻想的。这主要是因为眼睛擅长丢弃无意义的类别,例如“定制商店”和“小说完成”。
我尝试对类别列表进行重复数据删除,或者只查看所有 3 本书出现的类别,但它仍然是相当垃圾的数据。我希望您能想到如何将这些数据转化为对用户有意义的东西。
到目前为止,我最好的想法就是扫描数据并匹配一个硬编码列表。所以像:
if Count("scientific & Fantasy") > 3 then list is sci fi if Count("business Finance & law") > 3 then list is business
等等
虽然这是非常严格的,理想情况下我想构建一些更灵活/强大的东西。
欢迎所有建议。
我认为这是一个高级问题,因此不应受到我如何调用 API 的影响,但作为参考,我使用的是 Python/Appengine/Webapp。
谢谢
汤姆
在多次撞到桌子后进行更新,我已经成功地解决了这个问题,令我满意。这并不复杂,但我已经编写了一些 python 代码来满足我的需求。我欢迎任何人改进我的代码或提供建议。
基本上,代码背后的逻辑是这样的: 1) 在 XML 树中,开始的节点的底部节点(书籍 > 主题)是对这本书实际内容的最佳猜测。例如:http ://www.amazon.co.uk/Surface-Detail-Iain-M-Banks/dp/1841498939/它返回“科幻小说”。答对了。2) 通常,通过将自己限制在那些开始的结果(书籍>主题)中,会丢弃很多好的信息。因此,3)我尝试获取类似书籍的列表并将类别从它们中拉出,如果失败,那么我只获得分配给原始书籍的类别。
也许最好的解释是给你下面的代码:
#takes as input the xml output of the amazon api browsenodes call
def getcategories(xml):
#fetches the names of all the nodes, stores them in a list
categories = []
for book in xml.getElementsByTagName('BrowseNode'):
category = get_text(book,'Name')
categories.append(category)
#turn the one list into a series of individual lists
#each individual list should be a particular tree from browsenode
#each list will end 'Books'
#the first item in the list should be the bottom of the tree
taglists = []
while 'Books' in categories:
find = categories.index('Books') + 1
list = categories[:find]
taglists.append(list)
for word in list:
categories.remove(word)
#now, we only return the first item from a list which contains 'Subjects'
final = []
for tagset in taglists:
while 'Subjects' in tagset:
final.append(tagset[0])
tagset.pop(tagset.index('Subjects'))
return final
class Browsenodes(webapp.RequestHandler):
def get(self):
#get the asin of the target book
asin = self.request.get('term')
if book_title:
#fetch the amazon key
api = API(AWS_KEY, SECRET_KEY, 'uk', processor=minidom_response_parser)
try:
#try getting a list of similar books - note the response group set to browsenodes
result = api.similarity_lookup(asin, ResponseGroup='BrowseNodes')
except:
#there aren't always a list of similar books, so as a failsafe just get the book I wanted.
result = api.item_lookup(asin, ResponseGroup='BrowseNodes')
final = getcategories(result)
#turn it into a set to de-dupe multiple listings of the same category
self.response.out.write(set(final))
给你一个输出的味道:
书: http ://www.amazon.co.uk/Surface-Detail-Iain-M-Banks/dp/1841498939/
标签:当代小说产品太空歌剧科幻小说
http://www.amazon.co.uk/Godel-Escher-Bach-Eternal-anniversary/dp/0140289208/ 心理学 数学史 数学逻辑 通用 AAS 流行数学 科学、技术与医学 艺术与音乐 心灵哲学 亚马逊数学架构& 逻辑当代哲学:1900-逻辑经典物理玄学物理哲学通用技术代数数论人工智能科学史
http://www.amazon.co.uk/Flatland-Romance-Dimensions-Dover-Thrift/dp/048627263X/ 当代小说数学哲学 通用 AAS 流行数学哲学 科学、技术和医学心理哲学 科幻数学当代哲学: 1900- 代数数论 产品 经典 形而上学与远见 神话与童话 拓扑学 一般主题 一般理论方法 形而上学 人工智能 科学史
http://www.amazon.co.uk/Victoria-Condor-Books-Knut-Hamsun/dp/0285647598/ 当代小说 文学小说 心理学 一般 AAS 经典 短篇小说