beautifulsoupパッケージ内に、ユーザーがサイト内でクロールの深さを設定できる機能はありますか?私はPythonには比較的新しいですが、以前はRでRcrawlerを使用していました.Rcrawlerは 'MaxDepth'を提供しているので、クローラはそのドメイン内のホームページから一定数のリンク内に入ります。BeautifulSoupでのクロールの深さ
Rcrawler(Website = "https://stackoverflow.com/", no_cores = 4, no_conn = 4, ExtractCSSPat = c("div"), ****MaxDepth=5****)
Pythonで私の現在のスクリプトの基本は、ページ上のすべての可視テキストを解析しますが、私はクロールの深さを設定したいと思います。
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/)がここでうまく合うと信じています。 –