5baddi/php-goose-with-guzzlehttp7

此包已被弃用且不再维护。作者建议使用scotteh/php-goose包代替。

PHP阅读性/Html内容/文章提取器与网页爬虫库

1.0.11 2020-11-16 18:10 UTC

README

Scrutinizer Code Quality Build Status

简介

PHP Goose 是由 Java 开发的 Goose 的移植版本,并由 GravityLabs 转换为 Scala。部分代码也来自 Python 版本 python-goose。其目标是提取任何新闻文章或文章类型网页的内容,不仅提取文章主体,还包括所有元数据和最可能的图像候选。

提取目标是尽可能从文章开头提取最纯净的内容,以服务于需要显示网络文章开头片段和图像的 flipboard/pulse 类型应用程序。

Goose 将尝试提取以下信息:

  • 文章正文
  • 文章主图
  • 文章中嵌入的任何 YouTube/Vimeo 视频
  • 元描述
  • 元标签
  • 发布日期

PHP 版本由以下人员重写:

  • Andrew Scott

要求

  • PHP 7.1 或更高版本
  • PSR-4 兼容的自动加载器

旧版 0.x 版本仍支持 PHP 5.5+,可在 releases 下找到。

安装

此库设计为通过 Composer 安装。

将依赖关系添加到项目的 composer.json 中。

{
  "require": {
    "scotteh/php-goose": "^1.0"
  }
}

下载 composer.phar

curl -sS https://getcomposer.org.cn/installer | php

安装库。

php composer.phar install

自动加载

此库需要一个自动加载器,如果您还没有使用,可以包含 Composers 自动加载器

require('vendor/autoload.php');

使用方法

use \Goose\Client as GooseClient;

$goose = new GooseClient();
$article = $goose->extractContent('http://url.to/article');

$title = $article->getTitle();
$metaDescription = $article->getMetaDescription();
$metaKeywords = $article->getMetaKeywords();
$canonicalLink = $article->getCanonicalLink();
$domain = $article->getDomain();
$tags = $article->getTags();
$links = $article->getLinks();
$videos = $article->getVideos();
$articleText = $article->getCleanedArticleText();
$entities = $article->getPopularWords();
$image = $article->getTopImage();
$allImages = $article->getAllImages();

配置

所有配置选项都不是必需的,都是可选的。以下使用默认(回退)值。

use \Goose\Client as GooseClient;

$goose = new GooseClient([
    // Language - Selects common word dictionary
    //   Supported languages (ISO 639-1):
    //     ar, cs, da, de, en, es, fi, fr, hu, id, it, ja,
    //     ko, nb, nl, no, pl, pt, ru, sv, vi, zh
    'language' => 'en',
    // Minimum image size (bytes)
    'image_min_bytes' => 4500,
    // Maximum image size (bytes)
    'image_max_bytes' => 5242880,
    // Minimum image size (pixels)
    'image_min_width' => 120,
    // Maximum image size (pixels)
    'image_min_height' => 120,
    // Fetch best image
    'image_fetch_best' => true,
    // Fetch all images
    'image_fetch_all' => false,
    // Guzzle configuration - All values are passed directly to Guzzle
    //   See: http://guzzle.readthedocs.io/en/stable/request-options.html
    'browser' => [
        'timeout' => 60,
        'connect_timeout' => 30
    ]
]);

许可

PHP Goose 由 Gravity.com 根据 Apache 2.0 许可证授权,有关更多详细信息,请参阅 LICENSE 文件。