【问题标题】:how to parse the meta tag in the webpage [duplicate]如何解析网页中的元标记[重复]
【发布时间】:2011-08-19 21:32:52
【问题描述】:

标签: php meta-tags robots.txt


【解决方案1】:
function get_meta($url)
{
    // Get & Tidy HTML
    $tidy = new tidy();
    $tidy->parseFile($url, array("output-html" => true));
    $tidy->cleanRepair();
    // Parse XML
    $xml = new DOMDocument();
    $xml->loadHTML($tidy);
    $meta_tags = $xml->getElementsByTagName("meta");
    // Put meta informations in an array
    $meta = array();
    foreach($meta_tags as $meta_tag)
    {
        $key = $meta_tag->hasAttribute("http-equiv") ? $meta_tag->getAttribute("http-equiv") : $meta_tag->getAttribute("name");
        $value = $meta_tag->hasAttribute("content") ? $meta_tag->getAttribute("content") : $meta_tag->getAttribute("value");
        $meta[$key] = $value;
    }
    return $meta;
}

print_r(get_meta("http://php.net/manual/fr/tidy.cleanrepair.php"));

【讨论】:

  • 在上面的代码中,你提到了我应该在哪里传递网站的 url...?每个或 $url 中的正确方法是什么?
  • $url 包含你要解析的网页的URL,字符串格式。该函数返回元标记的关联数组,您可以使用 print_r 函数对其进行检查:fr2.php.net/manual/fr/function.print-r.php
  • 如何正确传递上面代码中的url可以指导一下
  • 我已经通过了上面代码中的 url 但它显示语法错误:-(
【解决方案2】:

你可以:

  1. 使用 file_get_contents 检索原始 HTML 数据

  2. Tidy HTML 代码使其更具可读性;如果您的 Web 服务器上没有安装 Tidy:

    apt-get install php5-tidy

  3. 使用 DOMDocument

  4. 解析元素

【讨论】:

  • @creadiff 谢谢你的明确想法,哥们你能不能给我那个代码,因为我已经尝试了很多次但它没有出现..
  • 您拥有所有正确的元素!为什么不至少尝试一下?
  • 你能检查一下上面的代码,如果可能的话请告诉我......
  • 此代码仅在极少数情况下有效,因为您要查找的字符串非常具体。
  • 哦,我知道它太难了,它在特定站点中工作,而不是在其他站点中的 exe
猜你喜欢
  • 2014-08-12
  • 1970-01-01
  • 2011-02-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多