Hướng dẫn php get element by attribute - php get phần tử theo thuộc tính

Tôi cần trích xuất một số dữ liệu từ một trang web với PHP. Phần mà tôi quan tâm có cấu trúc tương tự như thế này:

apple
cat
orange
dog
mango
monkey

Đầu tiên, tôi muốn khai thác tất cả các loại trái cây, và sau đó là tất cả các động vật, để tôi được nhóm lại.

Tôi đã tìm ra cách lặp qua tất cả các giá trị thuộc tính. Đây là mã:

$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";
}

Vì vậy, tôi nhận được:

fruit animal fruit animal fruit animal

Tôi cũng tìm ra cách lấy nội dung văn bản của các yếu tố:

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

Vì vậy, nếu được bao gồm trong vòng lặp và lặp lại, tôi nhận được:

apple cat orange dog mango monkey

Tôi cảm thấy như mình rất thân thiết, nhưng tôi không thể có được những gì tôi muốn. Tôi cần một cái gì đó như thế này:

Nếu (Target = "Fruit") sau đó cho tôi "Apple, Orange, Mango".

Có thể ai đó xin vui lòng chỉ cho tôi đi đúng hướng?

Thanks.

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    ĐọcDOMElement::getAttribute() function is an inbuilt function in PHP which is used to get the value of the attribute with name for the current node.

    Syntax:

    string DOMElement::getAttribute( string $name )

    Bàn luận This function accepts a single parameter $name which holds the name of the attribute.

    DOMELEMENT :: Hàm getAttribution () là một hàm sẵn có trong PHP được sử dụng để có được giá trị của thuộc tính có tên cho nút hiện tại. This function returns an string value containing the attribute value.

    Tham số: Hàm này chấp nhận một tham số $ tên chứa tên của thuộc tính.DOMElement::getAttribute() function in PHP:

    Giá trị trả về: Hàm này trả về một giá trị chuỗi chứa giá trị thuộc tính.

    Dưới đây các chương trình đã cho minh họa chức năng domelement :: getAttribution () trong PHP:

    $dom

    $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";
    }
    
    4

    $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";
    }
    
    5

    $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";
    }
    
    6
    $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";
    }
    
    7

    $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";
    }
    
    8

    Chương trình 1:

    fruit animal fruit animal fruit animal
    
    5
    $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";
    }
    
    0
    $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";
    }
    
    9
    fruit animal fruit animal fruit animal
    
    8
    fruit animal fruit animal fruit animal
    
    9
    fruit animal fruit animal fruit animal
    
    4

    $a->item($i)->textContent
    
    1
    fruit animal fruit animal fruit animal
    
    5
    $a->item($i)->textContent
    
    3

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

    Output:

    value

    Chương trình 2:

    $dom

    $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";
    }
    
    0
    $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";
    }
    
    1
    $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";
    }
    
    2

    $dom

    $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";
    }
    
    4

    $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";
    }
    
    5

    $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";
    }
    
    6
    apple cat orange dog mango monkey
    
    4

    $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";
    }
    
    6
    apple cat orange dog mango monkey
    
    6

    $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";
    }
    
    6
    apple cat orange dog mango monkey
    
    8

    $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";
    }
    
    8

    string DOMElement::getAttribute( string $name )
    0
    $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";
    }
    
    0$dom
    fruit animal fruit animal fruit animal
    
    22224

    $a->item($i)->textContent
    
    1
    string DOMElement::getAttribute( string $name )
    7
    $a->item($i)->textContent
    
    3

    string DOMElement::getAttribute( string $name )
    9
    value
    0
    string DOMElement::getAttribute( string $name )
    0
    value
    2
    $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";
    }
    
    9
    value
    4

    $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";
    }
    
    6
    $a->item($i)->textContent
    
    1
    $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";
    }
    
    9
    value
    8
    value
    9
    All the id values of divs are:
    div1
    div2
    div3
    0__71

    All the id values of divs are:
    div1
    div2
    div3
    3

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

    Output:

    All the id values of divs are:
    div1
    div2
    div3

    Tham khảo: https://www.php.net/manual/en/domelement.getattribute.php https://www.php.net/manual/en/domelement.getattribute.php


    All the id values of divs are:
    div1
    div2
    div3
    5

    All the id values of divs are:
    div1
    div2
    div3
    6

    All the id values of divs are:
    div1
    div2
    div3
    7

    All the id values of divs are:
    div1
    div2
    div3
    8

    All the id values of divs are:
    div1
    div2
    div3
    9

    0

    Làm thế nào để có được thuộc tính phần tử trong PHP?

    DOMELEMENT :: Hàm getAttribution () là một hàm sẵn có trong PHP được sử dụng để có được giá trị của thuộc tính có tên cho nút hiện tại. Tham số: Hàm này chấp nhận một tham số $ tên chứa tên của thuộc tính.DOMElement::getAttribute() function is an inbuilt function in PHP which is used to get the value of the attribute with name for the current node. Parameters: This function accepts a single parameter $name which holds the name of the attribute.

    Làm thế nào để tôi tìm thấy một yếu tố theo thuộc tính của nó?

    Sử dụng phương thức QuerySelector () để có phần tử DOM theo thuộc tính, ví dụ:tài liệu.Truy vấnSelector ('[data-id = "first"]').Phương thức QuerySelector sẽ trả về phần tử đầu tiên trong tài liệu phù hợp với thuộc tính được chỉ định., e.g. document. querySelector('[data-id="first"]') . The querySelector method will return the first element in the document that matches the specified attribute.

    Làm thế nào để có được phần tử DOM trong PHP?

    DOMDocument :: GetEuityById () hàm là một hàm sẵn có trong PHP được sử dụng để tìm kiếm một phần tử có ID nhất định.Tham số: Hàm này chấp nhận một tham số $ ElementID duy nhất giữ ID để tìm kiếm.Giá trị trả về: Hàm này trả về Domelement hoặc NULL nếu phần tử không được tìm thấy.DOMDocument::getElementById() function is an inbuilt function in PHP which is used to search for an element with a certain id. Parameters:This function accepts a single parameter $elementId which holds the id to search for. Return Value: This function returns the DOMElement or NULL if the element is not found.

    Thuộc tính PHP là gì?

    Các thuộc tính là các loại lớp có thể được sử dụng để thêm siêu dữ liệu vào các lớp, hàm, phương thức lớp, thuộc tính lớp, hằng số và tham số lớp khác.Các thuộc tính không làm gì trong thời gian chạy.Các thuộc tính không có tác động đến mã, nhưng có sẵn cho API phản xạ.kinds of classes that can be used to add metadata to other classes, functions, class methods, class properties, constants, and parameters. Attributes do nothing during runtime. Attributes have no impact on the code, but available for the reflection API.