我正在写一个代码来从arxiv页面中提取摘要,例如页面http://arxiv.org/abs/1207.0102,我感兴趣的是从“我们研究一个模型...”中提取文本。到"...compass-Heisenberg模型“我的代码目前看起来像这样
$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标记中的所有内容。有没有办法只提取摘要?
发布于 2015-08-16 05:38:09
最好的选择是使用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
谢谢。
https://stackoverflow.com/questions/32029583
复制相似问题