Search code examples
pythonpython-3.xfunctionhtml-parsing

Scheduling Python Script but just blinking


I am relatively new to python and i am experiencing some issues with schedule. I have a simple BeautifulSoup code which is parse some news title from usatoday.

    class Command(BaseCommand):
    def handle(self,  *args, **options):
        html = urlopen('')
        soup = BeautifulSoup(html, 'html.parser')
        site_json=json.loads(soup.text)

   for i in range(len(site_json)):
                title = site_json[i]['title']

                try:
                    Job.objects.create(
                        title=title,)
                print('%s added' % (title,))
            except Exception as e: print(e)
            #   print('%s already exists' % (title,))
        self.stdout.write( 'job complete' )

My codes working perfectly but i tried to add schedule to run every 5sec. (its just testing)

import time
import schedule
from os import system

def sch():
    class Command(BaseCommand):
        def handle(self,  *args, **options):
            html = urlopen('')
            soup = BeautifulSoup(html, 'html.parser')
            site_json=json.loads(soup.text)

   for i in range(len(site_json)):
                title = site_json[i]['title']

                try:
                    Job.objects.create(
                        title=title,)
                print('%s added' % (title,))
            except Exception as e: print(e)
            #   print('%s already exists' % (title,))
        self.stdout.write( 'job complete' )

schedule.every(5).seconds.do(sch)

while True:
    schedule.run_pending()
    time.sleep(1)

In order to execute that py nothings happen and just blinking at shell. I dont have any output\error log. Thats because i dont know what to do. How do I resolve this issue?


Solution

  • You can use cronTab for having an interval in executing your script:

    import getpass
    from crontab import CronTab
     
    cron = CronTab(user=getpass.getuser())
    job = cron.new(command='python3 path/to/my/code.py')
    job.second.every(5)
     
    cron.write()