【问题标题】:How to store found data when starting script?启动脚本时如何存储找到的数据?
【发布时间】:2022-08-14 01:50:13
【问题描述】:

我目前编写了一个脚本,在其中我正在对自己的 Web API 进行某种监视。我的脚本的重点是我想做一种监视器,我每小时检查一次,看看是否有新数据已应用到我的 API 中,每当有新数据时,我希望它打印出来.

class Monitor:
    def __init__(self, url: str) -> None:
        self.url = url
        self.stored_id: set = set()

    def doRequest(self) -> None:
        while True:
            try:
                response = requests.get(
                    self.url,
                    timeout=12,
                )

                if response.status_code == 200:
                    parser = response.json()
                    if parser.get(\'configurations\', {}):
                        for configuration in parser[\'configurations\']:
                            if configuration[\'configuration\'][\'id\'] not in self.stored_id
                                upload_to_discord({
                                    \'art-number\': configuration[\'art\'],
                                     ...
                                })
                                self.stored_id.add(configuration[\'configuration\'][\'id\'])

                time.sleep(3600)

            except Exception as e:
                print(e)

但是我的问题是,每当我运行此脚本时,它总是会打印到我的不和谐 (upload_to_discord(dict)) 中,这意味着每当我重新启动脚本时,它总是会打印出来。我想知道如何跳过第一次迭代,而不是在第一次循环中打印出来,而是存储找到的数据,然后在第一次迭代之后,它将开始“监控”以查看是否添加了任何内容和每当有新数据时打印出来?

    标签: python python-3.x


    【解决方案1】:

    像这样的东西:

    class Monitor:
        def __init__(self, url: str) -> None:
            self.url = url
            self.stored_id: set = set()
    
        def doRequest(self) -> None:
            cnt = False
            
            while True:
                try:
                    response = requests.get(
                        self.url,
                        timeout=12,
                    )
    
                    if response.status_code == 200:
                        parser = response.json()
                        if parser.get('configurations', {}):
                            for configuration in parser['configurations']:
                                if (configuration['configuration']['id'] not in self.stored_id) and (cnt):
                                    upload_to_discord({
                                        'art-number': configuration['art']
                                        ...
                                    })
                                    self.stored_id.add(configuration['configuration']['id'])
                                    cnt = True
                                else:
                                    cnt = True
    
                    time.sleep(3600)
    
                except Exception as e:
                    print(e)
    

    【讨论】:

    • 如果我跟随,请不要退出,尝试做你说的但做不到。
    • 我已经编辑了我的答案,也许它更清楚?
    • 感谢。这给了我另一个解决方案,因为我可以简单地使用 true false 而不是 count
    • 很高兴它有帮助!我用cnt = Falsecnt = True 选项编辑了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多