从HTML内容中提取数据 [英] Extract the data from content of HTML
本文介绍了从HTML内容中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从HTML中提取数据。我用curl做了,但我需要的只是将标题传递给另一个变量:
< meta property =og :urlcontent =https://example.com/>
如何提取这个,还有更好的方法吗?
解决方案
您应该使用解析器将值从HTML文件/字符串/文档中提取出来。
$ string ='< meta property =og:urlcontent =https: //example.com/\">';
$ doc = new DOMDocument();
$ doc-> loadHTML($ string);
$ metas = $ doc-> getElementsByTagName('meta');
foreach($ metas as $ meta){
if($ meta-> getAttribute('property')=='og:url'){
echo $ meta-> getAttribute ('内容');
$ / code $ / pre
$ b $输出:
I'm trying to extract data from HTML. I did it with curl, but all I need is to pass the title to another variable:
<meta property="og:url" content="https://example.com/">
How to extract this, and is there a better way?
解决方案 You should use a parser to pull values out of HTML files/strings/docs. Here's an example using the domdocument.
$string = '<meta property="og:url" content="https://example.com/">';
$doc = new DOMDocument();
$doc->loadHTML($string);
$metas = $doc->getElementsByTagName('meta');
foreach($metas as $meta) {
if($meta->getAttribute('property') == 'og:url') {
echo $meta->getAttribute('content');
}
}
Output:
这篇关于从HTML内容中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文