注意Google News每篇文章摘录的底部都有来源。
The Guardian - ABC News - Reuters - Bloomberg
我正在尝试模仿。
例如,提交 URL http://www.washingtontimes.com/news/2010/dec/3/debt-panel-fails-test-vote/
我想返回 华盛顿时报
用 php 怎么可能?
最佳答案
我的答案正在扩展@AI W 使用页面标题的答案。下面是完成他所说的代码。
<?php
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];
}
}
//Example:
echo get_title("http://www.washingtontimes.com/");
?>
输出
Washington Times - Politics, Breaking News, US and World News
如您所见,这并不是 Google 所使用的,因此这让我相信他们获得了 URL 的主机名并将其与自己的列表相匹配。
http://www.washingtontimes.com/ => 华盛顿时报
关于php - 通过链接获取网站标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4348912/