2016-12-29 1 views
0

새 항목이 게시 될 때마다 웹 사이트에서 업데이트를 확인하고 싶습니다. 업데이트를 자주하지 않기 때문에 업데이트 할 때 매우 중요합니다. 관심있는 항목이 될 것입니다. "시작 번호"를 선택하고 페이지의 링크 수를 계산하여 10 분 간격으로 링크 수를 비교하여 링크 수가 시작 번호보다 커지도록하십시오. 수한 기능의 출력을 다른 기능의 입력으로 사용하십시오.

notify=True 
while notify: 
    try: 
     page = urllib.request.urlopen('web/site/url') 
     soup = bs(page, "lxml") 

     links=[] 
     for link in soup.findAll('a'): 
      links.append(link.get('href')) 

     if len(links) > start_num: 
      message = client.messages.create(to="", from_="",body="") 
      print('notified') 
      notify=False 
     else: 
      print('keep going') 
      time.sleep(60*5) 

    except: 
     print("Going to sleep") 
     time.sleep(60*10) 

방법 :

links=[] 
for link in soup.findAll('a'): 
    links.append(link.get('href')) 
start_num = len(links) 

그런 다음 지금 링크의 수에 해당 번호를 비교하고 5 초마다 :

우선 나는이 링크의 "시작 번호"를 얻기 위해 실행 나는이 링크를 현재의 링크 수와 비교할 때마다 덮어 쓰지 않고 링크의 시작 번호를 저장할 수있는 1 개의 함수로이 모든 것을 결합합니까?

+0

기능에서 상태를 유지하려면 클래스 사용을 고려해야합니다. –

답변

0

당신은 적어도 두 가지 방법으로 수행 할 수 장식 및 발전기

장식 :

def hang_on(func): 

    # soup should be in a visible scope 
    def count_links(): 
     # refresh page? 
     return len(soup.findAll('a')) 

    start_num = count_links() 

    def wrapper(*args, **kwargs): 
     while True: 
      try: 
       new_links = count_links() 
       if new_links > start_num: 
        start_num = new_links 
        return fund(*args, **kwargs) 
       print('keep going') 
       time.sleep(60*5)    
      except: 
       print("Going to sleep") 
       time.sleep(60*10)   

    return wrapper 

@hang_on  
def notify(): 
    message = client.messages.create(to="", from_="",body="") 
    print('notified') 

# somewhere in your code, simply: 
notify() 

발전기 :이 코드이기 때문에

def gen_example(soup): 

    # initialize soup (perhaps from url) 

    # soup should be in a visible scope 
    def count_links(): 
     # refresh page? 
     return len(soup.findAll('a')) 

    start_num = count_links() 

    while True: 
     try: 
      new_links = count_links() 
      if new_links > start_num: 
       start_num = new_links 
       message = client.messages.create(to="", from_="",body="") 
       print('notified') 
       yield True # this is what makes this func a generator 

      print('keep going') 
      time.sleep(60*5)    
     except: 
      print("Going to sleep") 
      time.sleep(60*10)  

# somewhere in your code: 
gen = gen_example(soup) # initialize 

gen.next() # will wait and notify 

# coming soon 
0

내가, 클래스로 구현하는 것이 아주 읽기 쉽고 지원하기 쉽습니다. 즐기기 :

class Notifier: 
    url = 'web/site/url' 
    timeout = 60 * 10 

    def __links_count(self): 
     page = urllib.request.urlopen(self.url) 
     soup = bs(page, "lxml") 

     links=[] 
     for link in soup.findAll('a'): 
      links.append(link.get('href')) 

     return len(links) 

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

    def run(self): 
     current_count = self.__links_count() 

     while True: 
      try: 
       new_count = self.__links_count() 

       if new_count > current_count: 
        self.__notify() 
        break 

       sleep(self.timeout) 

      except: 
       print('Keep going') 
       sleep(self.timeout) 

notifier = Norifier() 
notifier.run() 
+0

그래서 함수 (self.url, self.timeout) 내에서 인수로 사용되는 셀은 항상 클래스에 정의 된 메서드 중 하나에서 정의되지 않은 변수를 참조합니까? – e1v1s