웹 크롤러를 만들기 위해 "파이썬을 사용한 컴퓨팅 소개"의 코드를 사용하고 있습니다. 내가 바라는 것은 Google이나 야후와 같은 특정 웹 사이트를 피하는 것입니다. 크기와 잠재력 때문에 나를 안드로메다로 안내 할 수 있습니다.파이썬 웹 크롤러 정보
따라서 특정 웹 페이지를 스크리닝하기 위해 self.prohibited 섹션을 만들었습니다. 그러나 작동하지 않습니다. 고칠 제안이 있습니까? 미리 감사드립니다. self.prohibited
는 항상이 성명에서 True
로 평가로
def analyze(url):
'''returns the list of http links
in absolute format in the web page with URL url'''
print('Visiting: ', url) # for testing
# obtain links in the web page
content=urlopen(url).read().decode()
collector=Collector(url)
collector.feed(content)
urls = collector.getLink()
# compute word frequencies
content=collector.getData()
freq=frequency(content)
out=open('test.csv', 'a')
print(out, 'URL', 'word', 'count')
csv=writer(out)
#print the frequency of every text data word in web page
print('\n {:50}{:10}{:5}'.format('URL', 'word', 'count'))
for word in freq:
row1=(url, word, freq[word])
print('\n {:50} {:10} {:5}'.format(url, word, freq[word]))
csv.writerow(row1)
print('\n {:50} {:10}'.format('URL', 'link'))
for link in urls:
print('\n {:50} {:10}'.format(url, link))
row2=(url, link)
csv.writerow(row2)
return urls
class Crawler:
'a web crawler'
def __init__(self):
self.visited = set()
self.prohibited=['*google.com/*','*yahoo.com/*']
def crawl(self, url):
'''calls analyze() on web page url
and calls itself on every link to an univisted webpage'''
links=analyze(url)
self.visited.add(url)
for link in links:
if link not in self.visited and self.prohibited:
try:
self.crawl(link)
except:
pass
필자가 파이썬에서 복사하여 붙여 넣을 때이 기사를 쓰는 동안 그 부분이 삭제되거나 누락되었다고 생각합니다. –
내 대답은 여전히 올바른 코드 조각을 제공했습니다;) 나는 그것이 무엇을하는지 더 잘 설명하도록 업데이트했습니다. –
감사합니다. Valentin. –