php 简单的 HTML DOM 从标签中获取所有属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14456621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple HTML DOM getting all attributes from a tag
提问by TheEditor
Sort of a two part question but maybe one answers the other. I'm trying to get a piece of information out of an
有点像一个两部分的问题,但也许一个回答另一个。我试图从一个信息中获取一条信息
<div id="foo">
<div class="bar"><a data1="xxxx" data2="xxxx" href="http://foo.bar">Inner text"</a>
<div class="bar2"><a data3="xxxx" data4="xxxx" href="http://foo.bar">more text"</a>
Here is what I'm using now.
这是我现在正在使用的。
$articles = array();
$html=file_get_html('http://foo.bar');
foreach($html->find('div[class=bar] a') as $a){
$articles[] = array($a->href,$a->innertext);
}
This works perfectly to grab the href and the inner text from the first div class. I tried adding a $a->data1 to the foreach but that didn't work.
这非常适合从第一个 div 类中获取 href 和内部文本。我尝试向 foreach 添加 $a->data1 ,但这没有用。
How do I grab those inner data tags at the same time I grab the href and innertext.
我如何在抓取 href 和 innertext 的同时抓取这些内部数据标签。
Also is there a good way to get both classes with one statement? I assume I could build the find off of the id and grab all the div information.
还有一种用一个语句同时获得两个类的好方法吗?我假设我可以根据 id 构建查找并获取所有 div 信息。
Thanks
谢谢
回答by ermannob
To grab all those attributes, you should before investigate the parsed element, like this:
要获取所有这些属性,您应该在调查已解析的元素之前,如下所示:
foreach($html->find('div[class=bar] a') as $a){
var_dump($a->attr);
}
...and see if those attributes exist. They don't seem to be valid HTML, so maybe the parser discards them.
...看看这些属性是否存在。它们似乎不是有效的 HTML,因此解析器可能会丢弃它们。
If they exist, you can read them like this:
如果它们存在,您可以像这样阅读它们:
foreach($html->find('div[class=bar] a') as $a){
$article = array($a->href, $a->innertext);
if (isset($a->attr['data1'])) {
$article['data1'] = $a->attr['data1'];
}
if (isset($a->attr['data2'])) {
$article['data2'] = $a->attr['data2'];
}
//...
$articles[] = $article;
}
To get both classes you can use a multiple selector, separated by a comma:
要获得这两个类,您可以使用多个选择器,用逗号分隔:
foreach($html->find('div[class=bar] a, div[class=bar2] a') as $a){
...
回答by Tech Savant
I know this question is old, but the OP asked how they could get all the attributes in one statement. I just did this for a project I'm working on.
我知道这个问题很老,但是 OP 询问他们如何在一个语句中获取所有属性。我只是为我正在做的一个项目做这件事。
You can get all the attributes for an element with the getAllAttributes()method. The results are automatically stored in an array property called attr.
您可以使用该getAllAttributes()方法获取元素的所有属性。结果会自动存储在名为 的数组属性中attr。
In the example below I am grabbing all links but you can use this with whatever you want. NOTE: This also works with data-attributes. So if there is an attribute called data-urlit will be accessible with $e->attr['data-url']after you run the getAllAttributesmethod.
在下面的示例中,我抓取了所有链接,但您可以随心所欲地使用它。注意:这也适用于data-属性。因此,如果有一个被调用的属性data-url,则$e->attr['data-url']在您运行该getAllAttributes方法后将可以访问它。
In your case the attributes your looking for will be $e->attr['data1']and $e->attr['data2']. Hope this helps someone if not the OP.
在您的情况下,您要查找的属性将是$e->attr['data1']和$e->attr['data2']。希望这可以帮助某人,如果不是 OP。
Get all Attributes
获取所有属性
$html = file_get_html('somefile.html');
foreach ($html->find('a') as $e) { //used a tag here, but use whatever you want
$e->getAllAttributes();
//testing that it worked
print_r($e->attr);
}
回答by Stepan Chopko
Check this code
检查此代码
<?php
$html = file_get_html('somefile.html');
foreach ($html->find('a') as $e) {
$filter = $e->getAttribute('data-filter-string');
}
?>
回答by Bashirpour
$data1 = $html->find('.bar > a', 0)->attr['data1'];
$data2 = $html->find('.bar > a', 0)->attr['data2'];

