2015-10-10 62 views
0

我想讓腳本在特定時間每天自動運行一次或兩次,那麼最好的方法是什麼。Python使腳本每天在特定時間運行

def get_data(): 
    """Reads the currency rates from cinkciarz.pl and prints out, stores the pln/usd 
     rate in a variable myRate""" 

    sock = urllib.urlopen("https://cinkciarz.pl/kantor/kursy-walut-cinkciarz-pl/usd") 
    htmlSource = sock.read()        
    sock.close()           

    currancyRate = re.findall(r'<td class="cur_down">(.*?)</td>',str(htmlSource)) 

    for eachTd in currancyRate: 
     print(eachTd) 

    print currancyRate[0] 
    myRate = currancyRate[0] 
    print myRate 
    return myRate 

回答

0

您可以添加一個bash的功能。

while true; do <your_command>; sleep <interval_in_seconds>; done 
0

您可以使用crontab以定期運行任何腳本。見https://stackoverflow.com/a/8727991/1517864

要運行,每天一次的腳本(12:00),就需要這樣的條目在crontab

0 12 * * * python /path/to/script.py 
+0

我想代碼本身內做到這一點,但是這是一個想法 –

+0

那麼你可能要像這樣http://stackoverflow.com/a/16786600/1517864 – jayant

相關問題