繁体   English   中英

使用简单的html dom获取页面标题?

[英]getting title of page using simple html dom?

我正在尝试使用简单的html dom(TITLE标记之间的页面的标题)从外部站点获取标题,但未检索任何内容。 有任何想法吗?

$html = new simple_html_dom();
$html->load('http://www.google.com');
$titleraw = $html->find('title');
$title = $titleraw->title;
$html = new simple_html_dom();
$html->load_file('http://www.google.com'); 
$titleraw = $html->find('title',0);
$title = $titleraw->innertext;

$html->load_file()从文件或URL加载内容。

$html->find('title')将返回一个数组

$titleraw->innertext返回title元素的内容

->load()需要一个包含HTML而不是URL的字符串。

尝试:

$html = file_get_html('http://google.com');

代替。

除此之外,请注意,Google的ToS禁止屏幕抓取工具,因此希望您只是将该网址用作填充示例,而不是真正尝试抓取的内容。

只是

$mypage=file_get_html('http://myurl.com');
$title=$mypage->find('title',0);
echo $title->plaintext;

用这个

$html = new simple_html_dom();  
$html->load('http://www.google.com');
$titleraw = $html->find('title');
foreach($html->find('title') as $link_element) {        
    echo $link_element->plaintext;
}

而不是$title = $titleraw->title;

if(
  preg_match(
    '~<title>(.*)</title>~si',
    file_get_contents('http://www.google.com'),
    $result
  );
  var_dump($result[1]);
}else{ /* no result */ }

其他

$titleraw = $html->xpath('//title');

尝试

include_once 'simple_html_dom.php';
$oHtml = str_get_html($url);
$Title = array_shift($oHtml->find('title'))->innertext;
$Description = array_shift($oHtml->find("meta[name='description']"))->content;
$keywords = array_shift($oHtml->find("meta[name='keywords']"))->content;
echo $title;
echo $Description;
echo $keywords;

尝试这个

$html = new simple_html_dom()
$data = file_get_html('http://www.google.com/');

// Find all images
foreach($html->find('title') as $element)
       echo $element->plaitext . '<br>';

使用DOM和xpath,您可以执行以下操作:

function getTitle($url) {
   libxml_use_internal_errors(true);
   $doc = new DOMDocument();
   $doc->loadHTMLFile($url);
   $xpath = new DOMXPath($doc);
   $nlist = $xpath->query("//head/title");
   return $nlist->item(0)->nodeValue;
}
echo "Title: " . getTitle("http://www.google.com") . "\n";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM