首页 > 解决方案 > 如果使用 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

标签: python-requests

解决方案


您可以使用宜家网站也在使用的非官方库存 API,而不是抓取和分析 HTML。该 API 返回更易于分析的 JSON 数据,并且您还可以在产品恢复库存时获得估算值。

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

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

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

推荐阅读