如何从网页文本正文中提取前几句话

发布于 2024-12-01 00:54:25 字数 207 浏览 1 评论 0原文

我们正在构建某种挖掘网站,并希望自动获取有限的文本(2-3 句话)。可以是文章的最后 3 句。这样会更容易。目前我们获取网页内容没有问题,但想要制作通用脚本来获取几句话。我们希望避免为我们想要从中获取内容的每个网站制作自定义脚本。

我想找到由点组成的文本块。寻找近距离内的点,然后围绕点获取单词。这是原始的想法。有人有其他想法如何提取部分文本吗?

我们不想抓取完整的内容。

We are building some sort of digg site and want to automatically fetch limited text (2-3 sentences). It can be last 3 sentences of article.if that would be easier. At the momemt we fetch web page content without the problem but want to make universal script to get few sentences. We want to avoid making custom scripts for each web site from which we want to get content.

I was thinking to find the text block by dots. To find dots in a close range and then to get words around dots. That is raw idea. Does someone has some other idea how to extract just par of the text.

We don't want to scrape full content.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

输什么也不输骨气 2024-12-08 00:54:25

您可以查找文档中具有较少标记和较少垂直空白的大部分内容。下载页面的源代码并使用 strip_tags() 去除所有标记。然后,您可以使用正则表达式搜索五个连续的句子。

这是一个示例脚本。它使用未包含的类(curl_multi 函数的抽象),但该类与您的问题并不真正相关。

<?php

require_once("./../MultipleRequester.php");

$requester = new MultipleRequester();

$requester->addGetRequest( 'test', 'http://www.businessweek.com/news/2011-08-24/gold-tumbles-most-since-march-2008-as-demand-for-haven-wanes.html');
$requester->execute();
$content = $requester->getContent('test');

$plainText = strip_tags( $content );

$search = preg_match('/(\h{0,2}\v{0,2}\h{0,2}[A-Z]{1}[A-z0-9 ,\'")(.$]{10,1000}\.){2,5}/', $plainText, $matches);

if( $search )
    print trim($matches[0]);
else
    print "Could not extract anything.";

print "\n\n";
?>

这打印:

由于人们猜测美联储主席伯南克本周是否会表示央行愿意为经济提供更多刺激,美元兑一篮子六种主要货币上涨。各国央行行长本周在怀俄明州杰克逊霍尔举行会议,讨论美国经济复苏问题。

您可能仍然会遇到对内容进行大量标记的网站的问题。您可能想让正则表达式更加宽松,特别是对于空格。

正则表达式有点混乱,但您可以调整它或编写自己的正则表达式。

You could look for large portions of the document that have less markup and less vertical whitespace. Download the page's source and strip out any markup using strip_tags(). Then you can search for, say, five consecutive sentences using regular expressions.

Here's an example script. It uses a class not included (an abstraction of curl_multi functions), but that class isn't really relevant for your question.

<?php

require_once("./../MultipleRequester.php");

$requester = new MultipleRequester();

$requester->addGetRequest( 'test', 'http://www.businessweek.com/news/2011-08-24/gold-tumbles-most-since-march-2008-as-demand-for-haven-wanes.html');
$requester->execute();
$content = $requester->getContent('test');

$plainText = strip_tags( $content );

$search = preg_match('/(\h{0,2}\v{0,2}\h{0,2}[A-Z]{1}[A-z0-9 ,\'")(.$]{10,1000}\.){2,5}/', $plainText, $matches);

if( $search )
    print trim($matches[0]);
else
    print "Could not extract anything.";

print "\n\n";
?>

This prints:

The dollar rose against a basket of six major currencies amid speculation about whether Federal Reserve Chairman Ben S. Bernanke will say this week that the central bank is willing to provide more stimulus to the economy. Central bankers meet this week in Jackson Hole, Wyoming, to address the U.S. recovery.

You may still have trouble with sites that mark up their content a lot. You might want to make the regular expression more lenient, particularly towards whitespace.

The regexp is a little messy, but you can tune it or write your own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文