推荐学习书目
› 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
shiyuu
V2EX  ›  Python

requests 用 post 获取数据,当数值发生变化时打印记录

  •  
  •   shiyuu · Dec 22, 2021 · 3107 views
    This topic created in 1782 days ago, the information mentioned may be changed or developed.

    我现在用 post 获取到了数值,每 10 秒获取一次这个数值,当增大的时候就打印记录 比如现在是 a=50 ,如果变成了 55 ,那就打印输出增大了 5 ,然后重新赋值 a=55 就一直循环,要实现这个要怎么写?

    8 replies  •  2021-12-23 19:21:04 +08:00
    LoNeFong
    LoNeFong
    Dec 22, 2021
    1
    记录上一次数据
    C02TobNClov1Dz56
    C02TobNClov1Dz56
    Dec 22, 2021
    2
    threading.Timer 定时触发任务
    a 变量放到外面加 global 作为全局的变量
    lichdkimba
    lichdkimba
    Dec 22, 2021
    3
    死循环 time.sleep(10)
    SimonOne
    SimonOne
    Dec 22, 2021
    4
    10 秒循环
    post->变量新
    if 变量新 =/=变量旧
    变量旧 = 变量新
    打印(变量新-变量旧)
    endif.
    SimonOne
    SimonOne
    Dec 22, 2021
    5
    更正:
    10 秒循环
    post->变量新
    if 变量新 > 变量旧
    变量旧 = 变量新
    打印(变量新-变量旧)
    endif.
    killva4624
    killva4624
    Dec 22, 2021❤️1
    6
    a = init_a()
    while condition():
    result = get_result_with_request()
    if result > a:
    # log
    print(f"increase: { result - a}")
    a = result
    else:
    pass
    time.sleep(interval)
    shiyuu
    shiyuu
    OP
    Dec 22, 2021
    7
    @killva4624 感谢了,已经可以了,下午办公室实在太吵思考不下去,逻辑已经混乱了,看了你的才捋顺清楚
    eason1874
    eason1874
    Dec 23, 2021
    8
    得多累才会连这么简单的逻辑都能搞乱。。。

    注意休息,小命要紧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   808 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 17:22 · PVG 01:22 · LAX 09:22 · JFK 12:22
    ♥ Do have faith in what you're doing.