与keepa.com的API接口。
项目描述
这个Python库允许您与Keepa的API接口,以查询亚马逊产品信息和历史记录。它还包含一个绘图模块,允许绘制产品。
有关API定价信息,请参阅Keepa API。
文档可以在readthedocs上找到,网址为keepa 文档。
要求
此库与Python >= 3.8兼容,需要
numpy
aiohttp
matplotlib
tqdm
当安装了matplotlib时,可以从原始数据中绘制产品历史记录。
与keepa接口需要访问密钥和来自Keepa API的月度订阅。
安装
可以从PyPi安装模块
pip install keepa
源代码也可以从GitHub下载,并使用以下方式安装
cd keepa 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])
产品价格图
产品报价图
使用异步的简要示例
以下是一个示例,使用keepa.AsyncKeepa类获取产品并绘制其价格和报价历史记录。
>>> import asyncio
>>> import keepa
>>> product_parms = {'author': 'jim butcher'}
>>> async def main():
... key = '<REAL_KEEPA_KEY>'
... api = await keepa.AsyncKeepa().create(key)
... return await api.product_finder(product_parms)
>>> asins = asyncio.run(main())
>>> asins
['B000HRMAR2',
'0578799790',
'B07PW1SVHM',
...
'B003MXM744',
'0133235750',
'B01MXXLJPZ']
使用异步Keepa接口查询产品,产品ASIN为'B0088PUEPK'。
>>> import asyncio
>>> import keepa
>>> async def main():
... key = '<REAL_KEEPA_KEY>'
... api = await keepa.AsyncKeepa().create(key)
... return await api.query('B0088PUEPK')
>>> response = asyncio.run(main())
>>> response[0]['title']
'Western Digital 1TB WD Blue PC Internal Hard Drive HDD - 7200 RPM,
SATA 6 Gb/s, 64 MB Cache, 3.5" - WD10EZEX'
详细示例
导入接口并建立与服务器的连接
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
您也可以使用async/await的keepa
import keepa
accesskey = 'XXXXXXXXXXXXXXXX' # enter real access key here
api = await keepa.AsyncKeepa.create(accesskey)
单个ASIN查询(异步)
products = await api.query('059035342X')
从列表中查询多个ASIN
asins = ['0022841350', '0022841369', '0022841369', '0022841369']
products = api.query(asins)
从numpy数组中查询多个ASIN
asins = np.asarray(['0022841350', '0022841369', '0022841369', '0022841369'])
products = api.query(asins)
产品是包含产品数据的列表,每个成功结果来自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])
您可以使用offers参数获取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)
Buy Box统计信息
要加载已使用的Buy Box统计信息,您必须启用offers。此示例加载产品报价并将Buy Box数据转换为pandas.DataFrame。
>>> import keepa
>>> key = '<REAL_KEEPA_KEY>'
>>> api = keepa.Keepa(key)
>>> response = api.query('B0088PUEPK', offers=20)
>>> product = response[0]
>>> buybox_info = product['buyBoxUsedHistory']
>>> df = keepa.process_used_buybox(buybox_info)
datetime user_id condition isFBA
0 2022-11-02 16:46:00 A1QUAC68EAM09F Used - Like New True
1 2022-11-13 10:36:00 A18WXU4I7YR6UA Used - Very Good False
2 2022-11-15 23:50:00 AYUGEV9WZ4X5O Used - Like New False
3 2022-11-17 06:16:00 A18WXU4I7YR6UA Used - Very Good False
4 2022-11-17 10:56:00 AYUGEV9WZ4X5O Used - Like New False
.. ... ... ... ...
115 2023-10-23 10:00:00 AYUGEV9WZ4X5O Used - Like New False
116 2023-10-25 21:14:00 A1U9HDFCZO1A84 Used - Like New False
117 2023-10-26 04:08:00 AYUGEV9WZ4X5O Used - Like New False
118 2023-10-27 08:14:00 A1U9HDFCZO1A84 Used - Like New False
119 2023-10-27 12:34:00 AYUGEV9WZ4X5O Used - Like New False
贡献
通过以开发模式安装此存储库来为此存储库做出贡献,方法是使用fork此存储库
git clone https://github.com/<USERNAME>/keepa pip install -e .[test]
然后,您可以添加您的功能或提交您的错误修复,然后运行您的单元测试
pytest
单元测试将自动强制执行最低代码覆盖率标准。
接下来,为了确保您的代码符合最低代码风格标准,运行
pre-commit run --all-files
最后,从您的fork创建拉取请求,我将确保对其进行审查。
致谢
此Python模块由Alex Kaszynski和几位贡献者编写,基于Marius Johann(Keepa的首席执行官)编写的Java代码。Java源代码可在keepacom/api_backend找到。
许可证
Apache许可证,请参阅许可证文件。工作归功于Alex Kaszynski和Marius Johann。
项目详细信息
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。