14

我正在寻找一个包/模块/函数等,它大约是 Arc90 的 readability.js 的 Python 等价物

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

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

这样我就可以给它一些 input.html 并且结果是该 html 页面的“主要文本”的清理版本。我想要这个,以便我可以在服务器端使用它(与仅在浏览器端运行的 JS 版本不同)。

有任何想法吗?

PS:我已经尝试过 Rhino + env.js 并且该组合有效,但性能无法接受,清理大部分 html 内容需要几分钟时间 :( (仍然找不到为什么会有如此大的性能差异)。

4

6 回答 6

11

请尝试我的 fork https://github.com/buriy/python-readability,它速度快并且具有最新 javascript 版本的所有功能。

于 2011-06-16T15:34:21.517 回答
4

我们刚刚在 repustate.com 上推出了一个新的自然语言处理 API。使用 REST API,您可以清理任何 HTML 或 PDF 并仅取回文本部分。我们的 API 是免费的,所以请随意使用。它是在python中实现的。检查它并将结果与​​ readability.js 进行比较——我想你会发现它们几乎 100% 相同。

于 2010-05-31T19:47:57.543 回答
3

hn.py通过Readability 的博客App Engine 应用程序Readable Feeds使用了它。

我在这里将它捆绑为 pip 可安装模块:http: //github.com/srid/readability

于 2010-09-07T01:11:09.840 回答
1

我过去对此进行了一些研究,并最终在 Python 中实现了这种方法 [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()

在我使用的测试语料库中,它实际上运行良好,但要实现高可靠性需要进行大量调整。

于 2010-05-29T07:20:03.917 回答
0

为什么不尝试使用 Google V8/Node.js 而不是 Rhino?它应该是可以接受的快。

于 2010-05-27T13:51:31.413 回答
-3

我认为BeautifulSoup是 Python 最好的 HTML 解析器。但是您仍然需要弄清楚网站的“主要”部分是什么。

如果您只解析单个域,这相当简单,但要找到适用于任何站点的模式并不容易。

也许您可以将 readability.js 方法移植到 python?

于 2010-05-28T14:00:07.553 回答