首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用python监视文本站点(json)

使用python监视文本站点(json)
EN

Stack Overflow用户
提问于 2018-07-06 07:52:41
回答 1查看 68关注 0票数 0

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

我正在使用代码

代码语言:javascript
复制
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必须是整数“,所以我不太确定该怎么做。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-06 08:50:45

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

看看你现在的代码,我想你想要做的是将所有东西组合到一个for循环中。

另外,您不应该使用像Python这样的变量名,因为它遮蔽了内置的list函数list()

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51201325

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档