首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在php中提取meta元素的内容?

在php中提取meta元素的内容?
EN

Stack Overflow用户
提问于 2013-11-29 17:24:48
回答 3查看 92关注 0票数 0

我是一个全新的PHP开发新手,我想提取一个元标签的内容。

我有这段代码,它允许我提取# that元素的内容。

代码语言:javascript
复制
// Pull in PHP Simple HTML DOM Parser
include("simplehtmldom/simple_html_dom.php");

// Settings on top
$sitesToCheck = array(
                    // id is the page ID for selector
                    array("url" => "http://www.arsenal.com/first-team/players", "selector" => "#squad"),
                    array("url" => "http://www.liverpoolfc.tv/news", "selector" => "ul[style='height:400px;']")
                );
$savePath = "cachedPages/";
$emailContent = "";

// For every page to check...
foreach($sitesToCheck as $site) {
    $url = $site["url"];

    // Calculate the cachedPage name, set oldContent = "";
    $fileName = md5($url);
    $oldContent = "";

    // Get the URL's current page content
    $html = file_get_html($url);

    // Find content by querying with a selector, just like a selector engine!
    foreach($html->find($site["selector"]) as $element) {
        $currentContent = $element->plaintext;;
    }

    // If a cached file exists
    if(file_exists($savePath.$fileName)) {
        // Retrieve the old content
        $oldContent = file_get_contents($savePath.$fileName);
    }

    // If different, notify!
    if($oldContent && $currentContent != $oldContent) {


        // Build simple email content
        $emailContent = "Hey, the following page has changed!\n\n".$url."\n\n";
    }

    // Save new content
    file_put_contents($savePath.$fileName,$currentContent);
}

// Send the email if there's content!
if($emailContent) {
    // Sendmail!
    mail("me@myself.name","Sites Have Changed!",$emailContent,"From: alerts@myself.name","\r\n");
    // Debug
    echo $emailContent;
}

但我想要更改此代码,以获得income中的评论数量。

下面是meta标签,我将在其中提取评论的数量:

代码语言:javascript
复制
<meta item="desc" content="Comments:645">

我说得够清楚了吗,你听懂了吗?

如果我说得不够清楚,可以问我?

感谢你的帮助

EN

回答 3

Stack Overflow用户

发布于 2013-11-29 17:31:55

有两种方法可以做到这一点。您可以使用原生PHP函数:get_meta_tags(),如下所示:

代码语言:javascript
复制
$tags = get_meta_tags('http://yoursite.com');
$comments = $tags['desc'];

或者你可以使用RegEx,但是上面的方法会更实用。

票数 1
EN

Stack Overflow用户

发布于 2013-11-29 17:34:01

您正在寻找的可能是屏幕抓取。

这是像php、python或ruby这样的编程语言在内存中加载网站并使用各种选择器从其中获取内容的过程。屏幕抓取主要用于有大量有趣数据但没有json或xml的网站

在用谷歌搜索之后,我偶然发现了这篇文章:PHP equivalent of PyQuery or Nokogiri?

这篇文章解释了更多关于网络屏幕抓取的内容:http://en.wikipedia.org/wiki/Web_scraping

票数 1
EN

Stack Overflow用户

发布于 2013-11-29 17:30:47

查找使用domDocument

代码语言:javascript
复制
    $dom = new domDocument;
    $dom->loadHTML($htmlPage);
    $metas = $dom->documentElement->getElementsByTagName('meta');
    $ar = array();
    foreach ($metas as $meta) {
        $name = $meta->getAttribute('name');
        $value = $meta->getAttribute('content');
        $ar[$name] = $value;
    }
    print_r($ar); // print array meta-values
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20282239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档