Php 如何使用https://github.com/voku/simple_html_dom

Php 如何使用https://github.com/voku/simple_html_dom,php,simple-html-dom,Php,Simple Html Dom,我试图访问元h1和元描述文本使用。我可以访问h1,但不能访问元描述文本 php代码如下所示: use voku\helper\HtmlDomParser; require_once 'vendor/autoload.php'; $urls = array("https://example.org/a.php", "https://example.org/b.php", "https://example.org/c.php"); $urls_count = count($urls); fo

我试图访问元h1和元描述文本使用。我可以访问h1,但不能访问元描述文本

php代码如下所示:

use voku\helper\HtmlDomParser;

require_once 'vendor/autoload.php';

$urls = array("https://example.org/a.php", "https://example.org/b.php", "https://example.org/c.php");

$urls_count = count($urls);

for ($i = 0; $i < count($urls); $i++) {
    $dom = HtmlDomParser::file_get_html($urls[$i]);
    $h1 = $dom->findOne('h1')->innertext; //this returns, assuing there is only one h1
    $description = $dom->findOne("meta[name='description']")->innertext; // this returns nothing 
    echo '<div>
<p><strong><a href='.$urls[$i].'>'.$h1.'</a></strong>    </p><p>'.$description.'</p></div>';
}
使用voku\helper\htmlcomparser;
需要_once“vendor/autoload.php”;
$URL=数组(“https://example.org/a.php", "https://example.org/b.php", "https://example.org/c.php");
$urls\u count=count($urls);
对于($i=0;$ifindOne('h1')->innertext;//假设只有一个h1,则返回
$description=$dom->findOne(“meta[name='description']”->innertext;//这不会返回任何内容
回声'
.$description.

'; }
在这里您可以找到一个示例:

$templateHtml='1!'
所有的元信息都在头部分

'; $htmlTmp=htmlcomparser::str_get_html($templateHtml); foreach($htmlTmp->find('meta')作为$meta){ 如果($meta->hasAttribute('content')){ $meta_data[$meta->getAttribute('name')][]=$meta->getAttribute('content'); } } //转储内容 /**@noinspection ForgottenDebugOutputInspection*/ var_导出($meta_数据,false); /* [ “说明”=>[ “免费网络教程”, ], '关键字'=>[ “HTML、CSS、XML、JavaScript”, ], “作者”=>[ “拉尔斯·莫莱肯”, ], ] */
PS:如果您正在搜索示例,那么现在是查看测试的好时机,如果没有测试,那么您无论如何都不想使用库。:-)

$templateHtml = '
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="description" content="Free Web tutorials">
  <meta name="keywords" content="HTML,CSS,XML,JavaScript">
  <meta name="author" content="Lars Moelleken">
</head>
<body>
<p>All meta information goes in the head section...</p>
</body>
</html>
';
$htmlTmp = HtmlDomParser::str_get_html($templateHtml);
foreach ($htmlTmp->find('meta') as $meta) {
    if ($meta->hasAttribute('content')) {
        $meta_data[$meta->getAttribute('name')][] = $meta->getAttribute('content');
    }
}
// dump contents
/** @noinspection ForgottenDebugOutputInspection */
var_export($meta_data, false);
/*
[
    'description' => [
        'Free Web tutorials',
    ],
    'keywords' => [
        'HTML,CSS,XML,JavaScript',
    ],
    'author' => [
        'Lars Moelleken',
    ],
]
 */