使用DOM抓住网站的标题 [英] Grabbing title of a website using DOM
本文介绍了使用DOM抓住网站的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
通过链接获取网站的标题
如何提取网站的标题?
如何使用PHP DOM抓住网站的标题? (哪个是使用PHP抓取它的最佳方法?)
How can a website's title be grabbed using PHP DOM? (Which is the best way to grab it using PHP?)
推荐答案
您可以使用getElementByTagName(),因为技术上只有您的HTML中的单个标题属性,所以您可以抓住您在DOM中遇到的第一个。
You can use the getElementByTagName() since there is technically only a single title attribute in your html so you can just grab the first one you come across in DOM.
$title = '';
$dom = new DOMDocument();
if($dom->loadHTMLFile($urlpage)) {
$list = $dom->getElementsByTagName("title");
if ($list->length > 0) {
$title = $list->item(0)->textContent;
}
}
这篇关于使用DOM抓住网站的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文