• PHP使用readability提取任意网页正文内容


    Readability 是 Mozilla的Readability.js (https://github.com/mozilla/readability)的PHP移植。

    Readability 使用 DOMDocument 解析所有html文本,扫描文本节点并根据字数、链接和元素类型给出分数。
    然后它选择得分最高的元素并创建一个包含所有兄弟元素的新 DOMDocument。
    返回标题、作者、主要图片和文本内容,并用节点评分以丢弃无用的元素,如导航栏、空节点、广告、页脚或任何不是文本主体的东西。

    国人的Html2Article-php或许和它是很有关系的。以下是github上README的译文


    phpQuery

    https://code.google.com/archive/p/phpquery/
    https://github.com/TobiaszCudnik/phpquery
    https://github.com/phpquery/phpquery

    html源代码美化

    https://github.com/ivanweiler/beautify-html


    Html2Article-php

    php提取网页正文内容的例子/php提取任意网页正文内容
    比较远古的代码,更新于8年前
    https://www.cnblogs.com/swocn/p/6730715.html
    https://blog.csdn.net/qikexun/article/details/52791234
    http://www.poluoluo.com/jzxy/201412/324682.html

    readability.php

    比较远古的 fork

    https://github.com/mingcheng/php-readability
    https://github.com/ridcully


    该项目最初来源 https://code.google.com/p/arc90labs-readability
    已经废弃的项目 https://github.com/andreskrey/readability.php
    该项目已经转到 https://github.com/fivefilters/readability.php

    ● 介绍和运行原理

    Readability 是 Mozilla的Readability.js (https://github.com/mozilla/readability)的PHP移植。

    Readability 使用 DOMDocument 解析所有html文本,扫描文本节点并根据字数、链接和元素类型给出分数。
    然后它选择得分最高的元素并创建一个包含所有兄弟元素的新 DOMDocument。
    返回标题、作者、主要图片和文本内容,并用节点评分以丢弃无用的元素,如导航栏、空节点、广告、页脚或任何不是文本主体的东西。

    ● 要求

    https://www.php.net/manual/zh/book.dom.php

    PHP 7.0以上版本,并开启 dom、xml 和 mbstring 扩展

    $ sudo apt-get install php7.4-xml php7.4-mbstring php-7.4-libxml

    ● 使用方法

    composer require andreskrey/readability.php

    ●● 可用属性的列表

    文章标题: -> getTitle()。
    文章内容: -> getContent()。
    摘录: -> getExcerpt()。
    主要图片: -> getImage()。
    所有图片: -> getImages()。
    作者: -> getAuthor()。
    文本方向(ltr或rtl) : -> getDirection()。
    如果需要调整最终的HTML,你可以通过调用->getDOMDocument()获得结果的DOMDocument。

    ●● 样例:

    创建一个可读性类,并传递一个配置类,应该总是用一个 try/catch 块来包裹 ->parseparse()函数调用,因为如果 HTML 不能被正确解析,就会抛出一个 ParseException。

    use andreskrey\Readability\Readability;
    use andreskrey\Readability\Configuration;
    use andreskrey\Readability\ParseException;

    $readability = new Readability(new Configuration());

    $html = file_get_contents('http://your.favorite.newspaper/article.html');

    try {
    $readability->parse($html);
    echo $readability;
    } catch (ParseException $e) {
    echo sprintf('Error processing text: %s', $e->getMessage());
    }

    如果你想对输出有更精细的控制,只需一个一个地调用属性,用你自己的HTML来包装它。

    <h1><?= $readability->getTitle(); ?></h1>
    <h2>By <?= $readability->getAuthor(); ?></h2>
    <div class="content"><?= $readability->getContent(); ?></div>

    ● 配置选项

    ●● 可用配置选项。在使用本地设置器调用它们时,记得要把set放在前面。

    MaxTopCandidates: 默认值为5,最高级别候选人的最大数量。
    CharThreshold: 默认值500,认为文章被解析成功的最小字符量。
    ArticleByLine: 默认值为false,搜索文章的署名并将其从文本中移除。它将被移到文章的元数据中。
    StripUnlikelyCandidates:默认值为true,删除那些不太可能有相关信息的节点。对于调试或解析复杂或非标准的文章很有用。
    CleanConditionally: 默认值为true,在解析后删除某些节点,以返回一个更干净的结果。
    WeightClasses: 默认值为true,在评级阶段对类进行加权。
    FixRelativeURLs: 默认值为false,将相对的URLs转换为绝对的。像/test到http://host/test。
    SubstituteEntities: 默认值为false,禁用libxml的substituteEntities标志,将避免替换HTML实体,如 &aacute; 到 á.。
    NormalizeEntities: 默认值为false,将UTF-8字符转换为其HTML实体等价物。对解析混合编码的HTML很有用。
    OriginalURL: 默认值http://fakehost,文章中的原始URL,用于固定相对URL。
    SummonCthulhu: 默认值为false,通过regex删除所有<script>节点。

    ●● 样例:

    声明原始的URL的设置配置

    $configuration = new Configuration();
    $configuration
    ->setFixRelativeURLs(true)
    ->setOriginalURL('http://my.newspaper.url/article/something-interesting-to-read.html');

    向构造函数传递一个配置参数的数组

    $configuration = new Configuration([
    'fixRelativeURLs' => true,
    'originalURL' => 'http://my.newspaper.url/article/something-interesting-to-read.html',
    // other parameters ... listing below
    ]);

    ● 调试日志

    ●● 日志记录是可选的

    它依赖于PSR Log (https://github.com/php-fig/log)接口来定义允许的记录器类型。PSR Log开发版位于 https://github.com/Seldaek/monolog
    在日志中,您将找到有关已解析节点的信息、它们被删除的原因以及它们被认为与最终文章相关的原因。

    ●● 样例:

    // Using monolog

    $log = new Logger('Readability');
    $log->pushHandler(new StreamHandler('path/to/my/log.txt'));

    $configuration->setLogger($log);

    ● libxml(版本 2.9.4~2.9.9 ) 存在的已知问题

    Javascript 代码溢出到文本正文中
    &nbsp自动转换为空格
    自闭合标签,例如<br />自动扩展为<br></br.

    以上无法在 libxml 中禁用它。

    ● 安全

    在使用 Readability 的输出时使用HTML Purifier(https://github.com/ezyang/htmlpurifier)之类的清理程序库来避免脚本注入。

    ==================================

    PHP 采集获取指定网址的内容

    这个是更加远古的代码,更新时间:2010年01月05日
    https://www.cnblogs.com/jscs/p/13685838.html
    https://www.jb51.net/article/21733.htm

  • 相关阅读:
    iOS:抽屉侧滑动画两种形式(1、UIView侧滑 2、ViewController侧滑)
    深入浅出 React Native:使用 JavaScript 构建原生应用
    JQuery:通过noConflict()方法同时使用jQuery 和其他框架
    JQuery AJAX: 了解jQuery AJAX
    iOS:iOS开发非常全的三方库、插件等等
    JavaScript:实现瀑布流
    iOS:crash崩溃日志分析
    敏捷开发一千零一夜
    江恩交易战法
    苹果:贩卖高科技的美学体验
  • 原文地址:https://www.cnblogs.com/yisuo/p/15856270.html
Copyright © 2020-2023  润新知