मैंने अतीत में कुछ शोध किया है और पाइथन में this approach [pdf] को कार्यान्वित करना समाप्त कर दिया है। मैंने जो अंतिम संस्करण लागू किया वह एल्गोरिदम लागू करने से पहले कुछ सफाई करता था, जैसे सिर/स्क्रिप्ट/आईफ्रेम तत्वों, छुपे तत्वों आदि को हटाने, लेकिन यह इसका मूल था।
यहां "लिंक सूची" भेदभावकर्ता के एक (बहुत) निष्पक्ष कार्यान्वयन के साथ एक फ़ंक्शन है, जो टेक्स्ट अनुपात (यानी नेविगेशन बार, मेनू, विज्ञापन इत्यादि) के भारी लिंक वाले तत्वों को निकालने का प्रयास करता है:
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()
परीक्षण कॉर्पस पर मैंने इसका उपयोग वास्तव में काफी अच्छा काम किया, लेकिन उच्च विश्वसनीयता को प्राप्त करने के लिए बहुत सारी tweaking की आवश्यकता होगी।
स्रोत
2010-05-29 07:20:03
धन्यवाद, अद्यतन उत्तर –