나는 이것에 대해 몇 가지 연구를 해본 결과 파이썬에서 this approach [pdf]을 구현했다. 마지막으로 구현 한 버전은 헤드/스크립트/iframe 요소, 숨겨진 요소 등을 제거하는 것과 같은 알고리즘을 적용하기 전에 정리 작업을 수행했지만 이것이 핵심이었습니다.
여기에는 텍스트 링크 비율이 큰 요소 (예 : 탐색 모음, 메뉴, 광고 등)를 제거하려고 시도하는 "링크 목록"판별 자의 아주 단순한 구현 기능이 있습니다 :
def link_list_discriminator(html, min_links=2, ratio=0.5):
"""Remove blocks with a high link to text ratio.
These are typically navigation elements.
Based on an algorithm described in:
http://www.psl.cs.columbia.edu/crunch/WWWJ.pdf
:param html: ElementTree object.
:param min_links: Minimum number of links inside an element
before considering a block for deletion.
:param ratio: Ratio of link text to all text before an element is considered
for deletion.
"""
def collapse(strings):
return u''.join(filter(None, (text.strip() for text in strings)))
# FIXME: This doesn't account for top-level text...
for el in html.xpath('//*'):
anchor_text = el.xpath('.//a//text()')
anchor_count = len(anchor_text)
anchor_text = collapse(anchor_text)
text = collapse(el.xpath('.//text()'))
anchors = float(len(anchor_text))
all = float(len(text))
if anchor_count > min_links and all and anchors/all > ratio:
el.drop_tree()
실제로 사용 된 테스트 코퍼스에서는 매우 잘 작동하지만 높은 안정성을 얻으려면 많은 조정이 필요합니다.
감사합니다. 답변이 업데이트되었습니다. –