crscheid / php-article-extractor
PHP HTML文章提取器
Requires
- php: ~7.2
- detectlanguage/detectlanguage: 2.*
- fivefilters/readability.php: 2.1.0
- scotteh/php-goose: ^1.1
- thesoftwarefanatics/php-html-parser: ^1.8.0
Requires (Dev)
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-23 22:09:46 UTC
README
这是一个PHP的网页文章解析和语言检测库。这个库可以从网页中读取文章内容,移除所有HTML并提供纯文本,适合语音合成或机器学习过程。
对于我开发的一个项目,我发现许多现有的开源解决方案都是好的起点,但每个都有其独特的缺陷。这个库将三种不同的方法聚合为单一解决方案,同时增加了语言检测功能。
如何使用
此库通过packagist.org分发,因此您可以使用composer检索依赖项
composer require crscheid/php-article-extractor
通过URL调用
此库将尝试为您检索HTML。您只需创建一个ArticleExtractor类,并在其上调用parseURL
函数,传递所需的URL。
use Cscheide\ArticleExtractor\ArticleExtractor; $extractor = new ArticleExtractor(); $response = $extractor->processURL("https://www.fastcompany.com/3067246/innovation-agents/the-unexpected-design-challenge-behind-slacks-new-threaded-conversations"); var_dump($response);
processURL
函数返回一个包含标题、文本和与请求相关联的元数据的数组。如果文本为null
,则表示解析失败。以下应该是上述代码的输出。
如果库遵循重定向,则result_url
字段将不同。此字段表示重定向后实际检索的最终页面。
array(5) {
["parse_method"]=>
string(11) "readability"
["title"]=>
string(72) "The Unexpected Design Challenge Behind Slack’s New Threaded Conversations"
["text"]=>
string(8013) "At first blush, threaded conversations sound like one of the most thoroughly mundane features a messaging app could introduce.After all, the idea of neatly bundling up a specific message and its replies in ..."
["language_method"]=>
string(7) "service"
["language"]=>
string(2) "en"
["result_url"]=>
string(126) "https://www.fastcompany.com/3067246/innovation-agents/the-unexpected-design-challenge-behind-slacks-new-threaded-conversations"
}
通过HTML调用
如果您已经有了HTML,您可以使用parseHTML
函数,并使用通过相同逻辑处理过的HTML。
use Cscheide\ArticleExtractor\ArticleExtractor; $extractor = new ArticleExtractor(); $myHTML = <load from some source>; $response = $extractor->processHTML($myHTML); var_dump($response);
parseHTML
函数返回一个包含标题、文本和与请求相关联的元数据的数组。如果文本为null
,则表示解析失败。以下应该是上述代码的输出。
在这种情况下不包括result_url
字段,因为我们没有在过程调用期间尝试获取HTML。
array(5) {
["parse_method"]=>
string(11) "readability"
["title"]=>
string(72) "The Unexpected Design Challenge Behind Slack’s New Threaded Conversations"
["text"]=>
string(8013) "At first blush, threaded conversations sound like one of the most thoroughly mundane features a messaging app could introduce.After all, the idea of neatly bundling up a specific message and its replies in ..."
["language_method"]=>
string(7) "service"
["language"]=>
string(2) "en"
}
您还可以通过传递语言检测服务的密钥以及自定义User-Agent字符串来创建ArticleExtractor
类。更多信息请参阅以下内容。
选项
语言检测方法
语言检测是通过在HTML元数据中查找语言指定符或利用Detect Language服务来处理的。
如果可以检测文章的语言,则返回ISO 639-1格式的语言代码以及检测方法,分别位于language
和language_method
字段中。如果成功找到,则language_method
字段可能为html
或service
。
如果语言检测失败或不可用,这两个字段都将返回为null。
Detect Language需要使用API密钥,您可以在那里注册。但是,您也可以在不使用它的情况下使用此库。如果HTML元数据不包含有关文章语言的任何信息,则language
和language_method
将返回为null值。
要使用语言检测服务利用此库,通过传递您的Detect Language API密钥来创建ArticleExtractor
对象。
use Cscheide\ArticleExtractor\ArticleExtractor; $extractor = new ArticleExtractor('your api key');
设置用户代理
可以为出站请求设置用户代理。为此,将所需的用户代理字符串传递给构造函数,如下所示:
use Cscheide\ArticleExtractor\ArticleExtractor; $myUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.116 Safari/537.36"; $extractor = new ArticleExtractor(null, $myUserAgent);
强制读取方法
可以强制使用读取尝试的方法,无论是Readability、Goose还是我们的自定义处理方式。这在Readability或Goose对特定网站有特定问题时非常有用。
要强制方法,只需在构造函数中提供第三个参数即可。四种有效的方法是readability
、goose
、goosecustom
或custom
。
$extractor = new ArticleExtractor(null, null, "goose");
输出格式
从版本1.0开始,输出格式已更改,为标题提供换行符。这对于自然语言处理应用中确定句子边界非常重要。如果不希望这种行为,只需在需要的地方删除额外的换行符。
做出这一更改的原因是,当简单地删除标题和段落HTML元素时,常常出现标题和后续句子之间没有分隔的问题。
文本字段输出格式示例
\n
A database containing 250 million Microsoft customer records has been found unsecured and online\n
NurPhoto via Getty Images\n
A new report reveals that 250 million Microsoft customer records, spanning 14 years, have been exposed online without password protection.\n
Microsoft has been in the news for, mostly, the wrong reasons recently. There is the Internet Explorer zero-day vulnerability that Microsoft hasn't issued a patch for, despite it being actively exploited. That came just days after the U.S. Government issued a critical Windows 10 update now alert concerning the "extraordinarily serious" curveball crypto vulnerability. Now a newly published report, has revealed that 250 million Microsoft customer records, spanning an incredible 14 years in all, have been exposed online in a database with no password protection.\n
What Microsoft customer records were exposed online, and where did they come from?\n
运行测试
此发行版中包含单元测试,可以在安装依赖项后使用PHPUnit运行。推荐的做法是使用Docker进行此操作,这样您甚至不需要在系统上安装依赖项。
注意:请设置环境变量
DETECT_LANGUAGE_KEY
,并使用您的Detect Language密钥,以便单元测试中的语言检测能够正常工作。
安装依赖项
这将使用composer docker镜像下载需求。注意使用了--ignore-platform-reqs
,因为我们的某些依赖项目前还不支持PHP 8。
docker run --rm --interactive --tty --volume $PWD:/app composer --ignore-platform-reqs install
运行单元测试
这将在php 7.4命令行环境中运行我们下载的phpunit依赖项。
docker run -v $(pwd):/app -w /app -e DETECT_LANGUAGE_KEY=<yourapikey> --rm php:7.4-cli ./vendor/phpunit/phpunit/phpunit