我正在开发一个程序,从这个网站https://www.deadstock.ca/collections/new-arrivals/products/nike-air-max-1-cool-grey.json抓取变体ID
我正在使用代码
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)
我不知道在这里做什么才能抓取物品的名称。如果你访问这个链接,你会看到它的名字在“标题”下面。如果你能帮我解决这个问题那就太棒了。
我得到了错误消息"TypeError: string indices必须是整数“,所以我不太确定该怎么做。
发布于 2018-07-06 08:50:45
您现在最大的问题是,在检查项目是否在列表中之前,您正在将项目添加到列表中,因此所有内容都会返回到列表中。
看看你现在的代码,我想你想要做的是将所有东西组合到一个for循环中。
另外,您不应该使用像Python这样的变量名,因为它遮蔽了内置的list
函数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()
https://stackoverflow.com/questions/51201325
复制相似问题