是的,我正试图从网页上抓取链接。链接存储在数组$ links中。
我有没有办法从阵列中的每个链接获取标题(来自下面的函数)。那将是一个多维数组吗?我怎样才能做到这一点?
$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab
// grabs the urls from URL
$file = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
$links[] = url_to_absolute($URL, $theelement->href);
}
print_r($links);
function getTitle($links)
{
//change it for the original titles.
$str = file_get_contents("http://www.theqlick.com");
if ( strlen( $str )>0 ) {
preg_match( "/\<title\>(.*)\<\/title\>/", $str, $title );
return $title[1];
}
}
$metatitle = getTitle();
echo $metatitle;
答案 0 :(得分:1)
我没有安装正确的库来测试它,但它应该让你知道从哪里开始:
$links = Array();
$URL = 'http://www.theqlick.com'; // change it for urls to grab
// grabs the urls from URL
$file = file_get_html($URL);
foreach ($file->find('a') as $theelement) {
$link = array();
$link['url'] = url_to_absolute($URL, $theelement->href);
$link['title'] = getTitle($link['url']);
$links[] = $link;
}
print_r($links);
function getTitle($url)
{
$file = file_get_html($url);
$titles = $file->find('title');
if (is_array($titles)) {
return $titles[0];
} else {
return null;
}
}