How do I extract an HTML title with Perl?

前端 未结 7 1933
粉色の甜心
粉色の甜心 2021-01-27 07:55

Is there a way to extract HTML page title using Perl? I know it can be passed as a hidden variable during form submit and then retrieved in Perl that way but I was wondering if

7条回答
  •  时光说笑
    2021-01-27 08:52

    get the title name form the file.

                        my $spool = 0;
    
                        open my $fh, "<", $absPath or die $!; 
                        #open ($fh, "<$tempfile" );
                        # wrtie the opening brace
                        print WFL "[";
                while (<$fh>) {
                        # removes the new line from the line read
                            chomp;
                        # removes the leading and trailing spaces.
                        $_=~ s/^\s+|\s+$//g;
                # case where the  and  occures in one line
                # we print and exit in one instant
                    if (($_=~/$startstring/i)&&($_=~/$endstring/i)) {
    
                            print WFL "'";
    
                        my ($title) = $_=~ m/$startstring(.+)$endstring/si;
                            print WFL "$title";
                            print WFL "',";
                            last;
                            }
                # case when the  is in one line and  is in other line
    
                #starting  string is found in the line
                    elsif ($_=~/$startstring/i) {
    
                            print WFL "'";
                # extract everything after <title> but nothing before <title>       
                        my ($title) = $_=~ m/$startstring(.+)/si;
                            print WFL "$title";
                            $spool = 1;
                            }
                # ending string  is found
                    elsif ($_=~/$endstring/i) {
                # read everything before  and nothing above that                                
                        my ($title) = $_=~ m/(.+)$endstring/si;
                            print WFL " ";
                            print WFL "$title";
                            print WFL "',";
                            $spool = 0;
                            last;
                            }
                # this will useful in reading all line between  and 
                    elsif ($spool == 1) {
                            print WFL " ";
                            print WFL "$_";
    
                            }
    
                        }
            close $fh;
            # end of getting the title name
    

提交回复
热议问题