从网页中提取标题和摘要


Extracting the title and abstract from a webpage

我试图从arXiv页面中提取标题和摘要,例如http://arxiv.org/abs/1207.0102,我的代码目前看起来像

function get_title($url){
  $str = file_get_contents($url);
  if(strlen($str)>0){
    $str = trim(preg_replace('/'s+/', ' ', $str)); // supports line breaks inside <title>
    preg_match("/'<title'>(.*)'<'/title'>/i",$str,$title); // ignore case
    return $title[1];
  }
}
echo get_title("http://arxiv.org/abs/1207.0102");

当我运行这段代码时,出现了这个错误

警告:file_get_contents(http://arxiv.org/abs/1207.0102): failed to打开流:HTTP请求失败!HTTP/1.1 403禁止访问C: ' wamp ' www ' mysite ' index . php

当我尝试不同的url时,这个问题不会发生,例如http://www.washingtontimes.com/.

有人知道为什么会这样吗?

另外,是否可以从这个网页中提取摘要?

这是网站不允许空用户代理的响应:

HTTP/1.1 403 Forbidden
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head><title>403 Forbidden</title></head>
<body>
<h1>Access Denied</h1>
 <p>Sadly, your client does not supply a proper User-Agent,
 and is consequently excluded.</p>
 <p>We have an inordinate number of problems with automated scripts
 which do not supply a User-Agent, and violate the automated access
 guidelines posted at arxiv.org
 -- hence we now exclude them all.</p>
 <p>(In rare cases, we have found that accesses through proxy servers
 strip the User-Agent information. If this is the case, you need to contact
 the administrator of your proxy server to get it fixed.)</p>

<p>If you believe this determination to be in error, see
<b>http://arxiv.org/denied.html</b> for additional information.</p>
</body>
</html>

如果您使用例如用户代理"Mozilla/5.0 (Windows NT 6.1;三叉戟/7.0;rv:11.0) like Gecko"在你的请求中,它将工作:

$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);