beautifulsoup 패키지 내에 사용자가 사이트 내에서 크롤링 깊이를 설정할 수있는 기능이 있습니까? 저는 비교적 Python에 익숙하지 않지만 R에서 Rcrawler를 사용했습니다. Rcrawler는 'MaxDepth'를 제공하므로 크롤러는 해당 도메인 내의 홈페이지에서 특정 링크 수 이내로 이동합니다.BeautifulSoup로 크롤링 깊이
Rcrawler(Website = "https://stackoverflow.com/", no_cores = 4, no_conn = 4, ExtractCSSPat = c("div"), ****MaxDepth=5****)
파이썬에서 내 현재 스크립트의 기본은 페이지의 모든 눈에 보이는 텍스트를 분석하지만 난 크롤링 깊이를 설정하고 싶습니다.
from bs4 import BeautifulSoup
import bs4 as bs
import urllib.request
def tag_visible(element):
if element.parent.name in ['style', 'script', 'head', 'title', 'meta', '[document]']:
return False
elif isinstance(element,bs.element.Comment):
return False
return True
def text_from_html(body):
soup = BeautifulSoup(html, 'lxml')
texts = soup.findAll(text=True)
visible_texts = filter(tag_visible, texts)
return u" ".join(t.strip() for t in visible_texts)
html = urllib.request.urlopen('https://stackoverflow.com/').read()
print(text_from_html(html))
모든 통찰력이나 방향에 감사드립니다.
BeautifulSoup는 크롤링을위한 것이 아닌 파싱입니다. 나는 [Scrapy] (https://scrapy.org/)가 여기에 잘 어울릴 수 있다고 생각합니다. –