-2

i'm using this but i don't get anything

    my $maldet = "https://myanimelist.net/anime/35849/Darling_in_the_FranXX";
my $response = $ua->request(HTTP::Request->new(GET => $maldet));
my $rrs = $response->content;
while ($rrs =~ m/<span itemprop=\"name\">(.*?)<\/span>/g) {
my $ANIME_NAME = $1;
print item("ANIME NAME"),("$ANIME_NAME\n");
}

this original source code , and i want to get DATA

<span itemprop="name">DATA</span>

please help , what i need to edit in my script

haukex
  • 2,973
  • 9
  • 21
Mobrine Hayde
  • 365
  • 1
  • 2
  • 10
  • Are you sure it's a web service? It doesn't look like one – Federico Navarrete Apr 25 '18 at 11:23
  • 2
    Don't parse HTML with regular expressions. Also, you can shorten your LWP code to `$ua->get($url)`, no need to create your own HTTP::Request object if you don't do anything special with it. – simbabque Apr 25 '18 at 11:27
  • 1
    Please [edit] your question and include sample input. Tell us what's in `$ANIME_LINK` so we get to see the real website. – simbabque Apr 25 '18 at 11:28
  • ah its like : https://myanimelist.net/anime/35849/Darling_in_the_FranXX – Mobrine Hayde Apr 25 '18 at 11:48
  • If you want to process all matches in that loop you might consider adding the `c` flag to your regular expression. And, as some of these `...` span multiple lines, the `s` flag might also be useful to match these. (These flags go to the end: `m/(.*?)<\/span>/gcs`) – sticky bit Apr 25 '18 at 12:03

1 Answers1

2

Do yourself a favor and don't parse HTML with regexes. While there are several modules to parse HTML properly in Perl (e.g. HTML::Parser and HTML::TreeBuilder are two classics), Mojo::DOM has a nice interface and can be used together with Mojo::UserAgent:

use warnings;
use strict;
use Mojo::UserAgent;

my $LINK = "test.html";
my $ua = Mojo::UserAgent->new;
my $dom = $ua->get("https://www.example.com/$LINK")->result->dom;
$dom->find(q{ span[itemprop="name"] })->each(sub {
        my $text = $_->text;
        print "<$text>\n";
    });
haukex
  • 2,973
  • 9
  • 21
  • so to parse html its more simple to use Mojo::UserAgent – Mobrine Hayde Apr 25 '18 at 12:06
  • Yes, [`Mojo::DOM`](https://metacpan.org/pod/Mojo::DOM) is a pretty good HTML parser. The `->dom` method is returning a `Mojo::DOM` object, but you can parse HTML strings and files by using `Mojo::DOM` directly (its docs show you how). – haukex Apr 25 '18 at 12:09