使用 Python 监控文本站点 (JSON)



IM 正在开发一个程序以从本网站获取变体 ID https://www.deadstock.ca/collections/new-arrivals/products/nike-air-max-1-cool-grey.json

我正在使用代码

import json
import requests
import time

endpoint = "https://www.deadstock.ca/collections/new-arrivals/products/nike-air-max-1-cool-grey.json"
req = requests.get(endpoint)
reqJson = json.loads(req.text)
for id in reqJson['product']:
name = (id['title'])
print (name)

我不知道在这里该怎么做才能抓住项目的名称。如果您访问该链接,您将看到该名称位于"标题"下。如果你能帮我解决这个问题,那就太棒了。

我收到错误消息"类型错误:字符串索引必须是整数",所以我不太确定该怎么做。

您现在最大的问题是,在检查它们是否在列表中之前,您将项目添加到列表中,因此所有内容都像列表中一样返回。

现在看看你的代码,我认为你想做的是将事情组合成一个 for 循环。

另外,作为提醒,您不应该使用像list这样的变量名称,因为它会隐藏内置的 Python 函数list()

list = []  # You really should change this to something else
def check_endpoint():
endpoint = ""
req = requests.get(endpoint)
reqJson = json.loads(req.text)
for id in reqJson['threads']:  # For each id in threads list
PID = id['product']['globalPid']  # Get current PID
if PID in list:
print('checking for new products')
else:
title = (id['product']['title'])    
Image = (id['product']['imageUrl'])
ReleaseType = (id['product']['selectionEngine'])
Time = (id['product']['effectiveInStockStartSellDate'])
send(title, PID, Image, ReleaseType, Time)
print ('added to database'.format(PID))
list.append(PID)  # Add PID to the list
return
def main():
while(True):
check_endpoint()
time.sleep(20)
return
if __name__ == "__main__":
main()

最新更新