doushan1157 2015-01-21 06:37
浏览 83
已采纳

使用DOMDocument在PHP中刮取特定标记属性

I am trying to extract the content from 'meta' tag depending upon the 'property'. Like `

<meta name="keywords" content="9gag,fun,funny,lol,meme,GIF,wtf,omg,fail,video,cosplay,geeky,forever alone" />
<meta name="twitter:image" content="http://images-cdn.9gag.com/images/thumbnail-facebook/14198244_1420182794.8999_AmeJun_n.jpg" />
<meta property="og:title" content="I finished the manga last week, so I wanted to make my on &quot;What Naruto taught me&quot;" />
<meta property="og:site_name" content="9GAG" />
<meta property="og:url" content="http://9gag.com/gag/aGVqbvz" />

... ` so I want to get only those content having 'og'. Through a cURL request I have been able to get attributes.

$ch = curl("http://9gag.com/gag/aGVqbvz?ref=fsidebar");
$dom = new DOMDocument();
@$dom->loadHTML($ch);

//echo $ch;
$links = $dom->getElementsByTagName('meta');
//get no of tags or elements
echo $links->length;
echo '<pre>';
foreach ($links as $link) {
    echo $link->getAttribute("property");
    echo '<br>';
}

how can I get content specific to only particular property or name.

  • 写回答

1条回答 默认 最新

  • duanlao1552 2015-01-21 06:44
    关注

    XPath is your friend here. An expression like //meta[starts-with(@property, "og")]/@content will grab the content attributes for all meta elements that have a property attribute whose value begins with "og".

    Example:

    $xpath = new DOMXPath($dom);
    $query = '//meta[starts-with(@property, "og")]/@content';
    foreach ($xpath->query($query) as $node) {
        echo $node->value, "
    ";
    }
    

    Output:

    I finished the manga last week, so I wanted to make my on "What Naruto taught me"
    9GAG
    http://9gag.com/gag/aGVqbvz
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件