python-requests - 如果使用 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
解决方案
您可以使用宜家网站也在使用的非官方库存 API,而不是抓取和分析 HTML。该 API 返回更易于分析的 JSON 数据,并且您还可以在产品恢复库存时获得估算值。
甚至还有一个用 javascript / node 编写的项目,可以直接从命令行为您提供此类信息:https ://github.com/Ephigenia/ikea-availability-checker
您可以轻松查看荷兰所有商店中椅子的库存量:
npx ikea-availability-checker stock --country nl 20336841
推荐阅读
- revit-api - Revit Python 包装器
- android - Videoview跳过视频只播放最后一个
- vuejs2 - data() 中的错误:“TypeError:this.game.league_person_result[0] 未定义”
- gstreamer - 如何收听 oggdemux gstreamer 失败?
- c++ - 如何使用着色器将顶点着色为网格(如线框模式)?
- javascript - 无法在打字稿的 JointJS 元素中添加 HTML
- highcharts - 有没有办法将高图中的列配置为具有零点填充和最大点宽度?
- reactjs - 在 ReactJS 中在多列和多行中显示响应式文本
- filemaker - Xero 和 Filemaker 库存和采购订单集成
- arrays - 合并数组数据的唯一值