The text
method extracts the text content from the given element. This won’t include the child elements. This method is provided by the Mojo::DOM
module, which is an HTML/XML DOM parser with CSS selectors.
$dom->at('html element')->text
This method will return the extracted text from the given element.
Let’s look at an example of this:
The given HTML is as follows:
<div>
Inside div
<p id="a">Inside first paragraph </p>
<p id="b">Inside second paragraph</p>
</div>
If we extract the text content from the given element div
, we will get the following output for the given HTML:
Inside div
use 5.010;use Mojo::DOM;# Parse the htmlmy $dom = Mojo::DOM->new('<div>Inside div <p id="a">Inside first paragraph </p><p id="b">Inside second paragraph</p></div>');# Extract the text content for the given elementsay $dom->at('div')->text;
In the code snippet above:
Mojo::DOM
module.$dom
scalar.div
and print it to the console.