2016-09-26 6 views
-1

いくつかのオンラインツイート(Bucky)の助けを借りて、私はいくつかのテキストがウェブページ上にあるかどうかをチェックする単純なウェブスクレーパーを書くことができました。しかし私がしたいことは、毎時間コードを実行させることです。私はコードをホストする必要もあると思います私はいくつかの調査をしましたが、毎時間それを実行する適切な方法を見つけることができないようです。私の機能を1時間ごとに実行するには?

import requests 
from bs4 import BeautifulSoup 

def odeon_spider(max_pages): 
    page = 1 
    while page <= max_pages: 
     url = "http://www.odeon.co.uk/films/rogue_one_a_star_wars_story/16038/" + str(page) #stores url in variable 
     source_code = requests.get(url) #gets url and sets it as source_code variable 
     plain_text = source_code.text #stores plain text in plain_text variable 
     soup = BeautifulSoup(plain_text, "lxml") #create beautifulsoup object 
     div_content = soup.findAll("div", {"class": "textComponent"}) #finds all divs with specific class 
     for x in div_content: 
      find_para = str(x.find('p').text) #finds all paragraphs and stores them in variable 
      text_to_search = "Register to be notified" #set text to search to variable 
      if text_to_search in find_para: #checks if text is in find_para 
       print("No tickets") 
      else: 
       print("Tickets") 
     page += 1 


odeon_spider(1) 

ありがとう:ここに は、私がこれまで持っているコードです!

+7

Linuxマシンの場合は 'crontab'を使用してください –

+1

Linuxの場合はcronjobを使います。windowsの場合はtaskschedulerを使用できます –

答えて

4

最も簡単な方法は、次のようになります:

import time 

while True: 
    call_your_function() 
    time.sleep(3600) 

あなたがLinux上でこれを実行したい場合は、あなただけの

nohup python -u your_script_name & 

を入力することができ、その後、あなたのスクリプトがプロセスとして実行されます(場合。あなたはそれを殺さないで、ちょうどハングアップなしで走り続けます)。

+1

Pythonコードを修正しました。 Pythonでは 'While'は' while'、括弧もそこには存在しないはずです。私はLinuxシェルに '&'を追加しました。そうしないと、シェルはコントロールを戻しません。それよりもあなたのコードは大丈夫です。私はまだcronを代わりに使用します。 – ElmoVanKielmo

+0

すばらしい、ありがとう!私はこれを試み、あなたに知らせるでしょう。 – user1663396

+0

あなたの訂正に感謝します。 – Acepcs

関連する問題