繁体   English   中英

如何提取网页摘要?

[英]How to extract the abstract of webpage?

我正在编写代码以从arxiv页(例如http://arxiv.org/abs/1207.0102页)中提取摘要,我有兴趣将文本从“我们研究...的模型”提取为“ ...罗盘-海森堡模型。” 我的代码目前看起来像

$url="http://arxiv.org/abs/1207.0102";
$options = array(
  'http'=>array(
    'method'=>"GET",
    'header'=>"User-Agent: Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko\r\n"
  )
);
$context = stream_context_create($options);
$str = file_get_contents($url, false, $context);

if (preg_match('~<body[^>]*>(.*?)</body>~si', $str, $body))
{
    echo $body[1];
}

问题在于它提取了body标签中的所有内容。 有没有办法只提取摘要?

最好的选择是使用DOM解析器,php在http://php.net/manual/en/class.domdocument.php内置了一个解析器,但是也有很多类似的类。

使用DOM文档,您将执行以下操作:

<?php
  $doc = new DOMDocument();
  $doc->loadHTML("<html><body>Test<br></body></html>");
  $text = $doc->getElementById("abstract");
?>

另一个选择是使用正则表达式,这看起来就像您已经在做的一样。 如您所知,它有点凌乱,需要一些学习, http://www.regular-expressions.info/tutorial.html

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM