php 通过php中的属性值获取HTML元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8395523/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:36:26  来源:igfitidea点击:

get HTML element by attribute value in php

phphtmldom

提问by stillenat

I need to extract some data from a webpage with php. The part that I'm interested in is structured similarly to this:

我需要使用 php 从网页中提取一些数据。我感兴趣的部分的结构与此类似:

<a href="somepath" target="fruit">apple</a>
<a href="somepath" target="animal">cat</a>
<a href="somepath" target="fruit">orange</a>
<a href="somepath" target="animal">dog</a>
<a href="somepath" target="fruit">mango</a>
<a href="somepath" target="animal">monkey</a>

First, I want to extract all fruits, and then all animals, so that I have them nicely grouped.

首先,我想提取所有水果,然后提取所有动物,以便将它们很好地分组。

I figured out how to loop through all attribute values. Here's the code:

我想出了如何遍历所有属性值。这是代码:

$dom = new DOMDocument();
$html = file_get_contents('example.html');

@$dom->loadHTML($html);

$a = $dom->getElementsByTagName('a');

for ($i; $i < $a->length; $i++) {
$attr = $a->item($i)->getAttribute('target');

echo $attr . "\n";
}

So I get:

所以我得到:

fruit animal fruit animal fruit animal

I also found out how to get the elements' text content:

我还发现了如何获取元素的文本内容:

$a->item($i)->textContent

So, if included in loop and echoed, I get:

所以,如果包含在循环中并得到回应,我得到:

apple cat orange dog mango monkey

I feel like I'm very close, but I can't get what I want. I need something like this:

我觉得我很接近,但我不能得到我想要的。我需要这样的东西:

if ( target = "fruit") then give me "apple, orange, mango".

如果(目标=“水果”)然后给我“苹果,橙子,芒果”。

Can someone please point me in the right direction?

有人可以指出我正确的方向吗?

Thanks.

谢谢。

回答by alex

Just continueon targetattributes which aren't fruit, and then add the textContentof the elements to an array.

只是continuetarget不是的属性上fruit,然后将textContent元素的添加到数组中。

$nodes = array();

for ($i; $i < $a->length; $i++) {
    $attr = $a->item($i)->getAttribute('target');

    if ($attr != 'fruit') {
        continue;
    }

    $nodes[] = $a->item($i)->textContent;
}

$nodesnow contains all the nodes of the elements which have their targetattribute set to fruit.

$nodes现在包含target属性设置为的元素的所有节点fruit

回答by fardjad

use DOMXPathand queries:

使用DOMXPath和查询:

$doc = new DOMDocument();
$doc->Load('yourFile.html');

$xpath = new DOMXPath($doc);

$fruits = $xpath->query("//a[@target='fruit']");
foreach($fruits as $fruit) {
    // ...
}

$animals = $xpath->query("//a[@target='animal']");
foreach($animals as $animal) {
    // ...
}

See thisdemo.

看到这个演示。

回答by XMen

Make two array

制作两个数组

$fruits=array();
$animals=array();

t and in loop when you get .

t 并在循环中获得 .

if(target=='fruit') {
   array_push($fruits,$valueofelement);

} else if ($target=='animal') {
   array_push($animals,$valueofelement);
}