2016-12-31 134 views
0

所以有這個網站發佈我想在一天中的任意時間購買一段時間的東西,我想寫一些東西給我的手機發送消息時新的網址發佈到該網頁。在一個類內的函數內調用一個函數

我計劃通過計算頁面上的鏈接數量(因爲它很少更新),並每隔5分鐘檢查一次,然後在5分鐘之前檢查它,然後在5分鐘後檢查它是否是10在此之前的幾分鐘內,5分鐘後檢查它是在15分鐘之前...並且如果它大於原來的大小,請將消息發送到我的手機。這是我到目前爲止有:

class url_alert: 
    url = '' 

    def link_count(self): 
     notifyy=True 
     while notifyy: 
      try: 
       page = urllib.request.urlopen(self.url) 
       soup = bs(page, "lxml") 
       links=[] 
       for link in soup.findAll('a'): 
        links.append(link.get('href')) 
        notifyy=False 
       print('found', int(len(links)), 'links') 
      except: 
       print('Stop making so many requests') 
       time.sleep(60*5) 
     return len(links) 


    def phone(self): 
     self= phone 
    phone.message = client.messages.create(to="", from_="",body="") 
     print('notified') 


    def looper(self): 
     first_count = self.link_count() 
     print('outside while') 

     noty = True 
     while noty: 
      try: 
       second_count = self.link_count() 
       print('before compare') 

       if second_count == first_count: 
        self.phone() 
        noty = False 
      except: 
       print('not quite...') 
       time.sleep(60) 


alert = url_alert() 
alert.looper() 

作爲一個測試,我決定把if語句確定是否要發送消息作爲平等的,但在循環不停地運行。我是否以正確的方式調用循環函數中的函數?

+0

哪裏self.phone定義?我只看到自己的電話的定義。 – MathSquared

+0

另外,__phone的第一行是'self = phone',應該做什麼? – MathSquared

+0

這是我第一次爲我自己創建的項目編寫自己的課程。我認爲self = phone就像消息變量和電話功能之間的'連接'。 – e1v1s

回答

0

看起來你需要消除try塊,因爲它是現在,如果self.phone()需要一個例外,你將永遠不會離開環路

def looper(self): 
    first_count = self.link_count() 
    while True: 
     if first_count != self.link_count(): 
      self.phone() 
      break 
     time.sleep(60) 
相關問題