【问题标题】:Python If printed results are the same as before print no change otherwise print new results. Run again every 10 minsPython 如果打印的结果与之前相同,则打印没有变化,否则打印新的结果。每 10 分钟再跑一次
【发布时间】:2020-10-26 13:39:34
【问题描述】:

我想每 10 分钟运行一次此脚本,如果今天的结果相同,我不想再次打印它们,除非它们发生变化。这甚至可能吗?不,我不是程序员,这只是一种爱好。

我正在使用 Twilio 向我发送一条可用露营地的短信,但我不想每 10 分钟收到相同的短信。我删除了 Twilio 代码,因为它包含我的帐户信息。预先感谢您的任何帮助。下面是我的代码。

from datetime import datetime
import pandas as pd
import requests
from tabulate import tabulate


result = []
for unit_id in range(5095, 5099):
    resp = requests.get(
        f"https://calirdr.usedirect.com/rdr/rdr/fd/"
        f"availability/getbyunit/{unit_id}/startdate/2020-10-30/nights/30/true?").json()
    result.extend(resp)

filter_by = ['UnitId', 'StartTime', 'IsFree', 'IsWalkin']
df = pd.DataFrame(result)
df = df.filter(items=filter_by)
df['StartTime'] = df['StartTime'].apply(lambda d: datetime.fromisoformat(d).strftime("%Y-%m-%d"))
df = df[df['IsFree']]
df = df[~df['IsWalkin']]
df['UnitId'] = df['UnitId'].replace([5095], 'Site 81')
df['UnitId'] = df['UnitId'].replace([5096], 'Site 82')
df['UnitId'] = df['UnitId'].replace([5097], 'Site 83')
df['UnitId'] = df['UnitId'].replace([5098], 'Site 84')
df['UnitId'] = df['UnitId'].replace([5099], 'Site 85')
print(tabulate(df, headers=filter_by))

下面是运行代码的结果。

    UnitId    StartTime    IsFree    IsWalkin
--  --------  -----------  --------  ----------
62  Site 83   2020-11-01   True      False
80  Site 83   2020-11-19   True      False
89  Site 83   2020-11-28   True      False

Process finished with exit code 0

【问题讨论】:

标签: python json pandas dataframe


【解决方案1】:

这将运行程序,等待十分钟,检查之前的结果是否与当前的结果相同,如果是,则退出。所以你现在的部分是弄清楚,如何才能在第二天退出它:)

//编辑:我编辑了与您的评论对应的代码

from datetime import datetime
import pandas as pd
import requests
from tabulate import tabulate
import time

def main():
    result = []
    for unit_id in range(5095, 5099):
        resp = requests.get(
        f"https://calirdr.usedirect.com/rdr/rdr/fd/"
        f"availability/getbyunit/{unit_id}/startdate/2020-10-30/nights/30/true?").json()
        result.extend(resp)

    filter_by = ['UnitId', 'StartTime', 'IsFree', 'IsWalkin']
    df = pd.DataFrame(result)
    df = df.filter(items=filter_by)
    df['StartTime'] = df['StartTime'].apply(lambda d: datetime.fromisoformat(d).strftime("%Y-%m-%d"))
    df = df[df['IsFree']]
    df = df[~df['IsWalkin']]
    df['UnitId'] = df['UnitId'].replace([5095], 'Site 81')
    df['UnitId'] = df['UnitId'].replace([5096], 'Site 82')

    return tabulate(df, headers=filter_by)


res_before = ""
while True:
    res = main()
    if res != res_before:
        print(res)
        res_before = res
    else:
        print("nothing changed")
    time.sleep(600)

【讨论】:

  • 运行此代码时出错:return tabulate(df, headers=filter_by) ^ SyntaxError: 'return' outside function Process finished with exit code 1
  • 看起来它现在可以工作了,我不得不将结果 = [] 移到 def main() 上方。唯一可能是我不想编程结束我希望它每 10 分钟检查一次并在结果发生变化时打印结果。可以这样做吗?谢谢
  • 测试代码时似乎没有打印任何请求。
  • 好的,我只保留了'Site 81''Site 82',剩下的你要重新添加才对。检查网站本身,那里有什么变化吗?
猜你喜欢
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
  • 2023-01-17
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多