2015-01-28 10 views
-1

최근에 저는 간단한 웹 크롤러를 만드는 작업을 시작했습니다. 두 번 반복 된 코드는 완벽하게 작동했지만 오류 예외 처리 기능을 사용하여 클래스로 변환하려고 시도했지만 더 이상 컴파일되지 않았습니다.웹 크롤러 클래스가 작동하지 않습니다.

import re, urllib 
class WebCrawler: 
    """A Simple Web Crawler That Is Readily Extensible""" 
    def __init__(): 
     size = 1 
    def containsAny(seq, aset): 
     for c in seq: 
      if c in aset: return True 
     return False 

    def crawlUrls(url, depth): 
     textfile = file('UrlMap.txt', 'wt') 
     urlList = [url] 
     size = 1 
     for i in range(depth): 
      for ee in range(size): 
       if containsAny(urlList[ee], "http://"): 
        try: 
         webpage = urllib.urlopen(urlList[ee]).read() 
         break 
        except: 
         print "Following URL failed!" 
         print urlList[ee] 
        for ee in re.findall('''href=["'](.[^"']+)["']''',webpage, re.I): 
         print ee 
         urlList.append(ee) 
         size+=1 
         textfile.write(ee+'\n') 

myCrawler = WebCrawler 

myCrawler.crawlUrls("http://www.wordsmakeworlds.com/", 2) 

그리고 여기에 생성 된 오류 코드가 있습니다.

Traceback (most recent call last): 
    File "C:/Users/Noah Huber-Feely/Desktop/Python/WebCrawlerClass", line 33, in <module> 
    myCrawler.crawlUrls("http://www.wordsmakeworlds.com/", 2) 
TypeError: unbound method crawlUrls() must be called with WebCrawler instance as first argument (got str instance instead) 
+3

- 노트 괄호를 (당신은 또한 당신이 정의 된 다른 방법에 대해이 작업을 수행해야합니다.). – jonrsharpe

+0

내가 그랬을 때이 오류가 반환되었습니다. –

+0

TypeError : __init __()은 인수가 없습니다 (주어진 1 개) –

답변

1

두 가지 문제가 있습니다. 하나는 일이 개 라인 :

myCrawler = WebCrawler 

당신은 WebCrawler의 인스턴스를 생성하지 않습니다, 당신은 단지 (클래스의 별칭을 만드는, 기본적으로) 이름을 myCrawlerWebCrawler에 결합된다. 대신이 작업을 수행해야합니다

myCrawler = WebCrawler() 

그런 다음이 줄에 :

def crawlUrls(url, depth): 

파이썬 인스턴스 메소드는 메소드의 첫 번째 인수로 수신기를 가지고. 일반적으로 self이라고 부르지 만 기술적으로 원하는대로 부를 수 있습니다. 그래서 당신은에 메소드 서명 변경해야합니다 :

def crawlUrls(self, url, depth): 

는`myCrawler = WebCrawler()`해야

+0

이렇게하면 여전히 오류를 반환하고 self.containsAny()를 호출하여 containsAny() 함수를 사용할 때 오류가 발생합니다. 그러나 프로그램은 단순히 실행 한 다음 화면에 아무 것도 인쇄하지 않고 잠시 멈 춥니 다. –

+0

나는 여전히이 질문에 대한 답을 찾고있다. 오류를 발견 할 수 있다면, 많은 도움이 될 것이다. 감사! –

+0

@ NoahHuber-Feely : 스택 추적을 게시해야합니다. – mipadi

관련 문제