class Crawler1(object):
def __init__(self):
'constructor'
self.visited = []
self.will_visit = []
def reset(self):
'reset the visited links'
self.visited = []
self.will_visit = []
def crawl(self, url, n):
'crawl to depth n starting at url'
self.analyze(url)
if n < 0:
self.reset()
elif url in self.visted:
self.crawl(self.will_visit[-1],n-1)
else:
self.visited.append(url)
self.analyze(url)
self.visited.append(url)
self.will_visit.pop(-1)
self.crawl(self.will_visit[-1],n-1)
def analyze(self, url):
'returns the list of URLs found in the page url'
print("Visiting", url)
content = urlopen(url).read().decode()
collector = Collector(url)
collector.feed(content)
urls = collector.getLinks()
for i in urls:
if i in self.will_visit:
pass
else:
self.will_visit.append(i)
나는이 프로그램은 지금까지 "N"웹 크롤러 클래스
내가 확신하지만 내가 코드에 어떤 문제가 있는지 확실하지 않다 그것을 할 수 있습니다로 연결하는 일련의를 통해 실행 만 할 그것은 충분합니다. 힌트는 좋을 것이다.
예상 출력의 경우 N = 1 및 사이트 2와 Site3에 대한 링크가있는 사이트 개발에 : 당신은 특히 또 다른 페이지를 크롤링 결정하는 방법으로,이 행동해야하는 방법에 대해 신중하게 생각해야
Visiting [Site1]
Visiting [Site2]
Visiting [Site3]
프로그램을 어떻게 실행하며 지금까지 어떤 행동을 보았습니까? 나는'c = Crawler1(); c.crawl ('Site1', 3)'. – Edmund
정확히 그렇게. 그리고 내가 'visted Site1'다음 에러 코드 'AttributeError :'Crawler1 '객체에'visted '속성이 없습니다. –
"visted"에서'i '가 누락 되었기 때문에 그럴 수 있습니다.) 그 이후의 다른 오류 ? – Edmund