调用亚马逊插件keepa数据的API获取产品的历史信息

代码已上传 https://github.com/jevy146/keepa

1.https://keepa.com/#! 这个是keepa的官网,首先你需要注册购买其服务。相对于他能提供的亚马逊的大量数据,这些钱花的超值。
2.这里以日本站的数据获取为例。
https://www.amazon.co.jp/dp/B003G357L0
在这个插件中你可以获取历史的排名以及价格、评论的变化情况。
在这里插入图片描述
3.这里使用的是Python调用其API的数据,keepa的首席执行官给的开源文档是java写的,,果然java才是最强的编程语言。亚马逊后台给的API接口用的是php,java和.Net。
这里放出来的是亚纳逊的官方API开发文档接口:欢迎大家参与交流。
http://docs.developer.amazonservices.com/zh_CN/orders/2013-09-01/Orders_GetOrder.html
我收集的亚马逊对接api的链接,也有大佬用Python封装了接口

1. http://docs.developer.amazonservices.com/zh_CN/fba_outbound/FBAOutbound_Overview.html官方
2.https://blog.csdn.net/zhou_xtao/article/details/96109682
3.https://github.com/jevy146/amazon-demo  使用java调用亚马逊的API数据
4.https://github.com/mystudytime/easy-amazon-advertising  亚马逊对接广告数据
5.https://www.crifan.com/python_amazon_aws_api_lib_framework/ 调用api文档

4.进入正题:
这个是keepa官方给出的api接口代码。
https://github.com/keepacom/api_backend/
在这里插入图片描述
按照正常的操作:传入参数,相应的结果如下链接所示。这里作为java仅能看懂的菜鸟不作展开。
https://keepa.com/#!discuss/t/product-object/116
5.下面介绍以为Python语言的福音。有为大佬带着拯救世界的步伐过来了。
https://github.com/akaszynski/keepa
在这里插入图片描述
看到这个开源的代码,我的眼泪就要流下来了,好吧!
我们pip install keepa 安装这个包。包不算大。
查看其源码的,主要用的这个类和
parse_csv这个函数。在这里插入图片描述
在这里插入图片描述
6.那我们就按照大佬的代码走一遍。
首先去官网拿到你的api的钥匙,原谅我这里打上马赛克。。一个月15刀。
https://keepa.com/#!api

在这里插入图片描述
7.这里由于用的是日文,我就将作者的代码稍微修改了一下。已知大佬用的是Python中的matplotlib画图。
源码里面有。那就好办了。
在这里插入图片描述

import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
import keepa
#https://keepa.com/#!api  keepa数据的api
# enter real access key here
accesskey = '*****输入你自己的api钥匙******' 
#这里可以添加参数,后面再说。
api = keepa.Keepa(accesskey)
# Single ASIN query
products = api.query('B003G357L0',domain='JP') # returns list of product data
keepa.plot_product(products[0])

在这里插入图片描述
运行要花一些时间。
运行结果:这里我分段截图

在这里插入图片描述
在这里插入图片描述
这里我补充一下。
当你发送 api.query(‘B003G357L0’,domain=‘JP’)的请求时,keepa响应的结结果products是一个列表长度为1,这里我怎么说呢,我将这个数据保存到本地:

file=open('./out.txt','w',encoding="utf-8")
file.write(str(products[0]))
file.close()

在这里插入图片描述
这里获取products[0]的数据,里面就是一个字典了。
在这里插入图片描述
https://keepa.com/#!discuss/t/request-products/110/1 这里是官方给出请求参数的介绍
字典里面包含了很多信息。
在这里插入图片描述
这里你估计会想:我操这都是什么鬼,官方文档里面给出的我就不说了,这里说一下keepa这个包作者的思路。也就是这两个键的值
在这里插入图片描述
这里你不需要去解析product[‘csv’],作者是将这个解析了的,然后放到
product[‘data’]里面了。
我为啥这么说呢?我们看源码:这里作者使用parse_csv() 这个函数解析的。
在这里插入图片描述
我们看一下这个函数,源码我贴上来,这里传入一个列表(csv)


def parse_csv(csv, to_datetime=True, out_of_stock_as_nan=True):
    """Parses csv list from keepa into a python dictionary.

    Parameters
    ----------
    csv : list
        csv list from keepa

    to_datetime : bool, optional
        Modifies numpy minutes to datetime.datetime values.
        Default True.

    out_of_stock_as_nan : bool, optional
        When True, prices are NAN when price category is out of stock.
        When False, prices are -0.01
        Default True

    Returns
    -------
    product_data : dict
        Dictionary containing the following fields with timestamps:

        AMAZON: Amazon price history

        NEW: Marketplace/3rd party New price history - Amazon is
            considered to be part of the marketplace as well, so if
            Amazon has the overall lowest new (!) price, the
            marketplace new price in the corresponding time interval
            will be identical to the Amazon price (except if there is
            only one marketplace offer).  Shipping and Handling costs
            not included!

        USED: Marketplace/3rd party Used price history

        SALES: Sales Rank history. Not every product has a Sales Rank.

        LISTPRICE: List Price history

        5 COLLECTIBLE: Collectible Price history

        6 REFURBISHED: Refurbished Price history

        7 NEW_FBM_SHIPPING: 3rd party (not including Amazon) New price
            history including shipping costs, only fulfilled by
            merchant (FBM).

        8 LIGHTNING_DEAL:  3rd party (not including Amazon) New price
            history including shipping costs, only fulfilled by
            merchant (FBM).

        9 WAREHOUSE: Amazon Warehouse Deals price history. Mostly of
            used condition, rarely new.

        10 NEW_FBA: Price history of the lowest 3rd party (not
             including Amazon/Warehouse) New offer that is fulfilled
             by Amazon

        11 COUNT_NEW: New offer count history

        12 COUNT_USED: Used offer count history

        13 COUNT_REFURBISHED: Refurbished offer count history

        14 COUNT_COLLECTIBLE: Collectible offer count history

        16 RATING: The product's rating history. A rating is an
             integer from 0 to 50 (e.g. 45 = 4.5 stars)

        17 COUNT_REVIEWS: The product's review count history.

        18 BUY_BOX_SHIPPING: The price history of the buy box. If no
            offer qualified for the buy box the price has the value
            -1. Including shipping costs.

        19 USED_NEW_SHIPPING: "Used - Like New" price history
            including shipping costs.

        20 USED_VERY_GOOD_SHIPPING: "Used - Very Good" price history
            including shipping costs.

        21 USED_GOOD_SHIPPING: "Used - Good" price history including
            shipping costs.

        22 USED_ACCEPTABLE_SHIPPING: "Used - Acceptable" price history
            including shipping costs.

        23 COLLECTIBLE_NEW_SHIPPING: "Collectible - Like New" price
            history including shipping costs.

        24 COLLECTIBLE_VERY_GOOD_SHIPPING: "Collectible - Very Good"
            price history including shipping costs.

        25 COLLECTIBLE_GOOD_SHIPPING: "Collectible - Good" price
            history including shipping costs.

        26 COLLECTIBLE_ACCEPTABLE_SHIPPING: "Collectible - Acceptable"
            price history including shipping costs.

        27 REFURBISHED_SHIPPING: Refurbished price history including
            shipping costs.

        30 TRADE_IN: The trade in price history. Amazon trade-in is
            not available for every locale.

        31 RENT: Rental price history. Requires use of the rental 
            and offers parameter. Amazon Rental is only available
            for Amazon US.

    Notes
    -----
    Negative prices

    """
    # https://github.com/keepacom/api_backend
    # see api_backend/src/main/java/com/keepa/api/backend/structs/Product.java
    # [index in csv, key name, isfloat (is price)]
    indices = [[0, 'AMAZON', True],
               [1, 'NEW', True],
               [2, 'USED', True],
               [3, 'SALES', False],
               [4, 'LISTPRICE', True],
               [5, 'COLLECTIBLE', True],
               [6, 'REFURBISHED', True],
               [7, 'NEW_FBM_SHIPPING', True],
               [8, 'LIGHTNING_DEAL', True],
               [9, 'WAREHOUSE', True],
               [10, 'NEW_FBA', True],
               [11, 'COUNT_NEW', False],
               [12, 'COUNT_USED', False],
               [13, 'COUNT_REFURBISHED', False],
               [14, 'CollectableOffers', False],
               [15, 'EXTRA_INFO_UPDATES', False],
               [16, 'RATING', True],
               [17, 'COUNT_REVIEWS', False],
               [18, 'BUY_BOX_SHIPPING', True],
               [19, 'USED_NEW_SHIPPING', True],
               [20, 'USED_VERY_GOOD_SHIPPING', True],
               [21, 'USED_GOOD_SHIPPING', True],
               [22, 'USED_ACCEPTABLE_SHIPPING', True],
               [23, 'COLLECTIBLE_NEW_SHIPPING', True],
               [24, 'COLLECTIBLE_VERY_GOOD_SHIPPING', True],
               [25, 'COLLECTIBLE_GOOD_SHIPPING', True],
               [26, 'COLLECTIBLE_ACCEPTABLE_SHIPPING', True],
               [27, 'REFURBISHED_SHIPPING', True],
               [28, 'EBAY_NEW_SHIPPING', True],
               [29, 'EBAY_USED_SHIPPING', True],
               [30, 'TRADE_IN', True],
               [31, 'RENT', False]]

    product_data = {}

    for ind, key, isfloat in indices:
        if csv[ind]:  # Check if entry it exists
            if 'SHIPPING' in key:  # shipping price is included
                # Data goes [time0, value0, shipping0, time1, value1,
                #            shipping1, ...]
                times = csv[ind][::3]
                values = np.array(csv[ind][1::3])
                values += np.array(csv[ind][2::3])
            else:
                # Data goes [time0, value0, time1, value1, ...]
                times = csv[ind][::2]
                values = np.array(csv[ind][1::2])

            # Convert to float price if applicable
            if isfloat:
                nan_mask = values < 0
                values = values.astype(np.float)/100
                if out_of_stock_as_nan:
                    values[nan_mask] = np.nan

            if key == 'RATING':
                values /= 10

            timeval = keepa_minutes_to_time(times, to_datetime)
            product_data['%s_time' % key] = timeval
            product_data[key] = values

    return product_data

这里有个细节,作者在解析时间,这里的时间大家不要将其作为时间戳来解析。解析函数我po上来了。他这个时间是在’2011-01-01’的基础上加的。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
6.那排名是哪个键的值呢,别急,既然知道了作者给我们做了那么多的事,既然画图里面有用到排名的数据,又知道画图用的是product[‘data’]的值,还有这里的key的名称也是对应的。作者很贴心的。使用api的好处就是数据很干净。
在这里插入图片描述
在这里插入图片描述
7.那我们画一下这个图,对比对比。这里我画的图比较丑。

import matplotlib.pyplot as plt
plt.style.use('ggplot')# 设置绘图风格
plt.rcParams['font.sans-serif']=['SimHei'] #用来正常显示中文标签
plt.rcParams['axes.unicode_minus']=False #用来正常显示负号
# 设置图框的大小
fig = plt.figure(figsize=(20,6))
# 绘图
plt.plot(plot_data['SALES_time'], # x轴数据
         plot_data['SALES'], # y轴数据
         linestyle = '-', # 折线类型
         linewidth = 2, # 折线宽度
         color = 'steelblue', # 折线颜色
         marker = 'o', # 点的形状
         markersize = 6, # 点的大小
         markeredgecolor='black', # 点的边框色
         markerfacecolor='brown') # 点的填充色
# 添加标题和坐标轴标签
plt.title(product['title'])
plt.xlabel('Date')
plt.ylabel('Sales Rank')
# 剔除图框上边界和右边界的刻度
plt.tick_params(top = 'off', right = 'off')
# 为了避免x轴日期刻度标签的重叠,设置x轴刻度自动展现,并且45度倾斜
fig.autofmt_xdate(rotation = 45)
# 显示图形
plt.show()

还是比较相似的。
在这里插入图片描述
后面我肯定打算用pyecharts来作图的。
pyecharts代码合集。1.7.1版本的。

https://pyecharts.org/#/zh-cn/web_flask  flask-pyecharts 
https://www.kesci.com/home/project/5d371e2fcf76a60036fecc97  案例 pyecharts 和鲸社区
https://www.jianshu.com/p/065b1676a0ca
https://github.com/awsekfozc?tab=stars 
https://pyecharts.herokuapp.com/ pyecharts官网。
http://gallery.pyecharts.org/#/README pyecharts的示例代码

8.下面我们来解读一下keepa的数据,
frequentlyBoughtTogether =这个含义是顾客习惯这两个ASIN的一起买的,我比较看重的指标
features =是5点描述
在这里插入图片描述
catId =类目树(bestselller中有)的id,和类目名称
在这里插入图片描述
我还比较关心的是上架时间,这样可以看出他是不是新品。
这个产品的上架时间为2010/7/26
既然知道了是用keepa_minutes_to_time() 这个函数解析的。
在这里插入图片描述
那么将这个函数导入,使用即可。输出的结果与亚马逊上的一致。

from keepa import interface
listed_date =interface.keepa_minutes_to_time(product['listedSince'])
print(listed_date)

在这里插入图片描述

在这里插入图片描述

添加价格历史图表和在价格下降到所有亚马逊网站的选择。 ✜ Price History charts Detailed Price History charts for over 900 million Amazon products. ✜ Price Drop & Availability Alerts Set up a price watch directly from the product page. We track any product for you and notify you once the product dropped below your desired price. We track lightning deals as well! ✜ Compare and track international Amazon prices Prices for all Amazon locales are listed and can easily be tracked. ✜ Daily Deals, an overview of recent price drops Get the real deals. Keepa finds the best deals every day. Find products with the highest drop in price in your favorite product category. ✜ Wishlist Import You can easily import your whole wishlist and create price watches for every item on it. ✜ Customizable Change the behavior of the extension the way you like it. ✜ Amazon Locales Support [ .com | .co.uk | .de | .co.jp | .fr | .ca | .it | .es | .in | .com.mx | .com.br | .com.au ] ✜ Language Support [ English | Deutsch | 日本語 | Français | Italiano | Español | 中文 ] No account needed. Just install and start saving money. This extension does not add any browser buttons. You can access and set it up on any Amazon product page. We follows Chrome's minimum permissions policy and only request the following permissions: - Access to keepa.com: needed for communication with our servers to provide the extension with price history data and handle your (optional) user account for price tracking. - Access to amazon.com, amzn.com, amazon.co.uk, amazon.de, amazon.fr, amazon.it, amazon.ca, amazon.com.mx, amazon.es, amazon.cn, amazon.co.jp, amazon.in, amazon.com.br, amazon.nl and amazon.com.au: Required to embed our price history graph directly on Amazon product pages. - "notifications": Provides desktop notifications for price alerts. Only enabled if you explicitly choose this notification channel. - "webRequest" and "webRequestBlocking": These enable the extension to make requests on its own to the pages listed above. This communication en
评论 20
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_43351935

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值