如何从网页文本正文中提取前几句话
我们正在构建某种挖掘网站,并希望自动获取有限的文本(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以查找文档中具有较少标记和较少垂直空白的大部分内容。下载页面的源代码并使用
strip_tags()
去除所有标记。然后,您可以使用正则表达式搜索五个连续的句子。这是一个示例脚本。它使用未包含的类(curl_multi 函数的抽象),但该类与您的问题并不真正相关。
这打印:
您可能仍然会遇到对内容进行大量标记的网站的问题。您可能想让正则表达式更加宽松,特别是对于空格。
正则表达式有点混乱,但您可以调整它或编写自己的正则表达式。
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.
This prints:
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.