V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
QGabriel
V2EX  ›  Python

求大佬帮忙写个代码

  •  
  •   QGabriel · 2021-09-07 09:56:36 +08:00 · 2456 次点击
    这是一个创建于 933 天前的主题,其中的信息可能已经有所发展或是发生改变。
    需求如下:
    判断当前时间大于 9:30, 每分钟请求接口 10 点结束程序.

    谢谢了
    13 条回复    2021-09-07 15:46:03 +08:00
    easing
        1
    easing  
       2021-09-07 10:08:37 +08:00
    循环(当前时间大于 9:30 并且 当前时间小于 10 点){
    请求接口
    睡一分钟
    }
    fly2mars
        2
    fly2mars  
       2021-09-07 10:11:54 +08:00
    接口上定时任务
    princelai
        3
    princelai  
       2021-09-07 10:13:38 +08:00
    ```python
    from datetime import datetime
    now = datetime.now()
    now > datetime.fromisoformat(now.date().isoformat() + " 09:30")
    ```
    其他自己写,想要判断开盘时间应该很多环境都自带的

    你也可以考虑用 apscheduler 写个定时回调程序,9:30 开始,10 点结束,隔几分钟调用几次都能自己定,不过我觉得这么简单的东西没必要写成接口,写个方法封装起来不就好了吗
    ElegantOfKing
        4
    ElegantOfKing  
       2021-09-07 10:13:44 +08:00
    最简单的就是搜索关键字 cron 表达式 -> 0 30-59 9 * * ?
    humpy
        5
    humpy  
       2021-09-07 10:16:04 +08:00
    crontab -e

    30-59 9 * * * curl api
    QGabriel
        6
    QGabriel  
    OP
       2021-09-07 10:59:55 +08:00
    @princelai
    import time
    import requests
    import json

    def foo (code):
    print(0)

    while True:
    foo()
    time.sleep(10)

    我这样写 print 为什么一直不打印出来?
    princelai
        7
    princelai  
       2021-09-07 11:04:55 +08:00
    @QGabriel #6 应该会报缺少参数错误吧,如果 foo(xxx)这么写应该没问题,要么就是你的 stdout,stderr 出现了问题,最后不建议 print,你怎么也用个 logging 吧,不过我推荐 loguru,你这水平我建议先多看看基础,别着急写东西
    QGabriel
        8
    QGabriel  
    OP
       2021-09-07 11:13:27 +08:00
    @princelai 嗯 老哥说的没错 我会点 node 所以想直接上手 python.看了一下不是程序问题是 vscode 里在终端执行就没问题了.我一直用的是 run code 方式
    princelai
        9
    princelai  
       2021-09-07 11:18:16 +08:00
    @QGabriel #8 vscode 配置环境还是略微有点麻烦的,下个 pycharm-CE,免费够用,我觉得比 vscode 好用
    QGabriel
        10
    QGabriel  
    OP
       2021-09-07 11:33:15 +08:00
    @princelai 下个试试 谢谢了!
    AmberJiang
        11
    AmberJiang  
       2021-09-07 11:37:11 +08:00
    @QGabriel 你这个前面定义了一个 foo 的函数,还给了参数 code 但是你后面运行时候又没有给参数值 。。。所以肯定不会打印出来东西啊。。。
    RUGUAN
        12
    RUGUAN  
       2021-09-07 11:44:57 +08:00
    量化?
    Riye
        13
    Riye  
       2021-09-07 15:46:03 +08:00
    0 30/1 9-10 * * ? 支持 cron 表达式的定时任务 9 点到 10 点 30 分钟开始每分钟执行一次
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3312 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 13:20 · PVG 21:20 · LAX 06:20 · JFK 09:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.