如果使用 JavaScript,如何使用 python 请求创建股票可用性检查器?



我写了一些代码,应该检查产品是否重新库存,当有库存时,给我发一封电子邮件通知我。当我正在寻找的东西在 html 中时,这有效。

但是,有时某些对象是通过JavaScript加载的。如何编辑我的代码,以便网络抓取也适用于 JavaScript?

这是我到目前为止的代码:

import time
import requests
while True:
# Get the url of the IKEA page
url = 'https://www.ikea.com/nl/nl/p/flintan-bureaustoel-vissle-zwart-20336841/'
# Get the text from that page and put everything in lower cases
productpage = requests.get(url).text.lower()
# Set the strings that should be on the page if the product is not available
outofstockstrings = ['niet beschikbaar voor levering', 'alleen beschikbaar in de winkel']
# Check whether the strings are in the text of the webpage
if any(x in productpage for x in outofstockstrings):
time.sleep(1800)
continue
else:
# send me an email and break the loop

与其抓取和分析 HTML,您还可以使用宜家网站正在使用的非官方股票 API。该API返回JSON数据,这更容易分析,当产品恢复库存时,您还将获得估计值。

甚至还有一个用javascript/node编写的项目,它直接从命令行为您提供此类信息:https://github.com/Ephigenia/ikea-availability-checker

您可以在荷兰的所有商店轻松查看椅子的库存量:

npx ikea-availability-checker stock --country nl 20336841

最新更新