mydream
mydream

Reputation: 11

HTML DOMParser in Laravel

I want to get content of 'p' tag from site http://www3.nhk.or.jp/news/html/20191121/k10012185811000.html

p tag has 'content--summary' & 'content--summary-more' class, but I can't get data this my code

        $htmlurl = 'http://www3.nhk.or.jp/news/html/20191121/k10012185811000.html';
        $dom = new \DOMDocument();
        $dom->loadHTMLFile($htmlurl);
        $data = $dom->getElementsByTagName('p');

Can you tell me how to get text please. I'm using laravel 6.2

Upvotes: 1

Views: 5826

Answers (1)

Ruslan Skaldin
Ruslan Skaldin

Reputation: 1101

Try this one:

$htmlurl = 'http://www3.nhk.or.jp/news/html/20191121/k10012185811000.html';
$dom = new \DOMDocument();
$dom->loadHTMLFile($htmlurl);
$data = $dom->getElementsByTagName('p');
foreach( $data as $p ) {
    echo $p->textContent; // Print out content of <p> tags
    echo $p->getAttribute('class'); // Print out attribute's <p> classes
}

Upvotes: 2

Related Questions