Python Forum

Full Version: Creating multiple text fies from new url retrieve results
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

This is a similar question to what I posted earlier, however I've made some improvements and my function is (almost) doing what I want it to do. This code below is opening the same url that contains different data every time it opens, every 3 seconds and retrieving the text from it. However, every time the URL opens I would like a separate text file for the results, right now it updates the "testing.txt" file and saves the latest result from when I close exit the function.

I'd like a separate file for each 3 second interval, but if all results are stored into one master text file that works for me as well. Any and all help is welcome, thank you!!

import webbrowser
import schedule
import time
import urllib


def job():
    webbrowser.open('http://127.0.0.1:56781/mavlink/')

    urllib.urlretrieve('http://127.0.0.1:56781/mavlink/', "testing.txt")
    

schedule.every(3).seconds.do(job)

while 1:
    schedule.run_pending()
    time.sleep(1)
The following is a modified example from the APScheduler that should do the trick.
I have not tested it, so you may find a bug or two
the scheduler can be downloaded with:
pip install APScheduler
code:
"""
This code is a modified version of the tornado scheduler from the APScheduler package examples
"""
import os

from tornado.ioloop import IOLoop
from apscheduler.schedulers.tornado import TornadoScheduler

COUNT = 1

def job():
    webbrowser.open('http://127.0.0.1:56781/mavlink/')
    filename = 'testing{}.txt'.format(COUNT)
    COUNT += 1
    urllib.urlretrieve('http://127.0.0.1:56781/mavlink/', filename)


if __name__ == '__main__':
    scheduler = TornadoScheduler()
    scheduler.add_job(job, 'interval', seconds=3)
    scheduler.start()
    print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))

    # Execution will block here until Ctrl+C (Ctrl+Break on Windows) is pressed.
    try:
        IOLoop.instance().start()
    except (KeyboardInterrupt, SystemExit):
        pass