与 keepa.com 的接口
项目描述
这个 Python 模块允许您与Keepa的 API 交互,以查询亚马逊产品信息和历史记录。它还包含一个绘图模块以允许绘制产品。
在Keepa API中查看 API 定价
可以在keepa 文档的 readthedocs 上找到文档。
要求
模块与 Python >= 3.6 兼容并且需要:
麻木的
aiohttp
matplotlib
tqdm
安装matplotlib后,可以从原始数据中绘制产品历史 。
与keepa 交互需要访问密钥和Keepa API的每月订阅
安装
可以从PyPi安装模块:
pip install keepa
源代码也可以从GitHub下载并使用: python setup.py install或pip install 安装。
简要示例
import keepa
accesskey = 'XXXXXXXXXXXXXXXX' # enter real access key here
api = keepa.Keepa(accesskey)
# Single ASIN query
products = api.query('B0088PUEPK') # returns list of product data
# Plot result (requires matplotlib)
keepa.plot_product(products[0])
<图>
产品价格图
</figcaption> </figure> <figure>产品优惠图
</figcaption> </figure>使用异步的简要示例
这是一个使用异步接口获取产品并绘制其价格和报价历史记录的示例:
import keepa
# establish interface with keepa (this is not a real key)
mykey = '0000000000000000000000000000000000000000000000000000000000000000'
api = await keepa.AsyncKeepa.create(mykey)
# plot product request
request = 'B0088PUEPK'
products = await api.query(request)
product = products[0]
keepa.plot_product(product)
详细示例
导入接口并建立与服务器的连接
import keepa
accesskey = 'XXXXXXXXXXXXXXXX' # enter real access key here
api = keepa.Keepa(accesskey)
单一 ASIN 查询
products = api.query('059035342X')
# See help(api.query) for available options when querying the API
您也可以使用 keepa 女巫异步 / 等待
import keepa
accesskey = 'XXXXXXXXXXXXXXXX' # enter real access key here
api = await keepa.AsyncKeepa.create(accesskey)
单个 ASIN 查询(异步)
products = await api.query('059035342X')
List 中的多个 ASIN 查询
asins = ['0022841350', '0022841369', '0022841369', '0022841369']
products = api.query(asins)
来自 numpy 数组的多个 ASIN 查询
asins = np.asarray(['0022841350', '0022841369', '0022841369', '0022841369'])
products = api.query(asins)
Products 是一个产品数据列表,每个来自 Keepa 服务器的成功结果都有一个条目。每个条目都是一个字典,其中包含来自Amazon的相同产品数据。
# Available keys
print(products[0].keys())
# Print ASIN and title
print('ASIN is ' + products[0]['asin'])
print('Title is ' + products[0]['title'])
原始数据包含在每个产品结果中。原始数据存储为字典,每个键与其相关的时间历史配对。
# Access new price history and associated time data
newprice = products[0]['data']['NEW']
newpricetime = products[0]['data']['NEW_time']
# Can be plotted with matplotlib using:
import matplotlib.pyplot as plt
plt.step(newpricetime, newprice, where='pre')
# Keys can be listed by
print(products[0]['data'].keys())
如果安装了matplotlib,也可以从模块中绘制产品历史记录
keepa.plot_product(products[0])
您可以使用 offer 参数获取一个 ASIN(或多个 ASIN)的报价历史记录。有关详细信息,请参阅请求产品中的文档。
products = api.query(asins, offers=20)
product = products[0]
offers = product['offers']
# each offer contains the price history of each offer
offer = offers[0]
csv = offer['offerCSV']
# convert these values to numpy arrays
times, prices = keepa.convert_offer_history(csv)
# for a list of active offers, see
indices = product['liveOffersOrder']
# with this you can loop through active offers:
indices = product['liveOffersOrder']
offer_times = []
offer_prices = []
for index in indices:
csv = offers[index]['offerCSV']
times, prices = keepa.convert_offer_history(csv)
offer_times.append(times)
offer_prices.append(prices)
# you can aggregate these using np.hstack or plot at the history individually
import matplotlib.pyplot as plt
for i in range(len(offer_prices)):
plt.step(offer_times[i], offer_prices[i])
plt.show()
如果您计划进行大量的同时查询,您可能希望使用 wait=False参数来加速查询。
products = await api.query('059035342X', wait=False)
学分
这个 Python 模块由 Alex Kaszynski 和几位贡献者编写,基于 keepa 首席执行官 Marius Johann 编写的 Java 代码。Java 源代码可以在api_backend找到。
执照
Apache 许可证,请参阅许可证文件。工作归功于 Alex Kaszynski 和 Marius Johann。