推荐学习书目
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 · 2750 views
    This topic created in 1622 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
        1
    LoNeFong  
       Dec 22, 2021
    记录上一次数据
    C02TobNClov1Dz56
        2
    C02TobNClov1Dz56  
       Dec 22, 2021
    threading.Timer 定时触发任务
    a 变量放到外面加 global 作为全局的变量
    lichdkimba
        3
    lichdkimba  
       Dec 22, 2021
    死循环 time.sleep(10)
    SimonOne
        4
    SimonOne  
       Dec 22, 2021
    10 秒循环
    post->变量新
    if 变量新 =/=变量旧
    变量旧 = 变量新
    打印(变量新-变量旧)
    endif.
    SimonOne
        5
    SimonOne  
       Dec 22, 2021
    更正:
    10 秒循环
    post->变量新
    if 变量新 > 变量旧
    变量旧 = 变量新
    打印(变量新-变量旧)
    endif.
    killva4624
        6
    killva4624  
       Dec 22, 2021   ❤️ 1
    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
        7
    shiyuu  
    OP
       Dec 22, 2021
    @killva4624 感谢了,已经可以了,下午办公室实在太吵思考不下去,逻辑已经混乱了,看了你的才捋顺清楚
    eason1874
        8
    eason1874  
       Dec 23, 2021
    得多累才会连这么简单的逻辑都能搞乱。。。

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