在漂亮的汤包中是否有允许用户在站点中设置爬行深度的功能?我对Python还比较陌生,但是我以前在R中使用过MaxDepth,而且爬虫提供了“”,所以爬虫将在一定数量的链接范围内从该领域的主页。
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))任何洞察力或方向都会受到赞赏。
https://stackoverflow.com/questions/47908372
复制相似问题