2010-05-27 117 views
12

我正在尋找一個封裝/模塊/功能等,也就是大約相當於Python的Arc90的readability.js的有沒有像Python那樣的readability.js?

http://lab.arc90.com/experiments/readability

http://lab.arc90.com/experiments/readability/js/readability.js

,這樣我可以給它一些input.html並且結果被清除了該html頁面的「正文」的版本。我希望這樣可以在服務器端使用它(與只在瀏覽器端運行的JS版本不同)。

任何想法?

PS:我已經試過Rhino + env.js和那個組合工程,但是性能是不可接受的,它需要幾分鐘來清理大部分html內容:(:(仍然無法找到爲什麼會有這麼大的性能差異)。

回答

0

爲什麼不嘗試使用谷歌的V8/Node.js的,而不是犀牛?它應該是足夠快。

+0

env.js是否在V8/Node.js上運行,以使我擁有類似瀏覽器的環境? – 2010-05-27 14:12:08

+0

這正是http://flockfeeds.com/所做的。 – 2010-09-07 05:37:32

-3

我覺得BeautifulSoup是蟒蛇最好的HTML解析器。但你仍然需要弄清楚什麼網站的「主要」部分是。

如果你只是解析一個域,這是相當直接的,但f插入一個適用於任何網站的模式並不那麼容易。

也許你可以將readability.js方法移植到python?

1

我已經在過去做了一些研究,最後在Python中實現this approach [pdf]。我實現的最終版本在應用算法之前也做了一些清理,例如刪除head/script/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() 

在我使用的測試語料庫上,它實際上工作得很好,但實現高可靠性需要大量的調整。

4

我們剛剛在repustate.com上推出了一種新的自然語言處理API。使用REST API,您可以清除任何HTML或PDF並僅取回文本部分。我們的API是免費的,因此可隨意使用您的內容。它在python中實現。檢查一下,並將結果與​​readability.js進行比較 - 我想你會發現它們幾乎是100%相同。

+0

嗯,看起來很有希望! ;-)我會試一試。有沒有嚴格的限制?我每天可以處理多少頁? – 2010-06-01 07:47:50

+0

哇,我只是用你的網站輸入一些網址,並且它完美地提取了文章。 – 2010-08-03 17:37:43