How do I extract an HTML title with Perl?

前端 未结 7 1924
粉色の甜心
粉色の甜心 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:30

    It's not clear to me what you are asking. You seem to be talking about something that could run in the user's browser, or at least something that already has an html page loaded.

    If that's not the case, the answer is URI::Title.

    0 讨论(0)
  • 2021-01-27 08:33

    I would use pQuery. It works just like jQuery.

    You can say:

    use pQuery;
    my $page = pQuery("http://google.com/");
    my $title = $page->find('title');
    say "The title is: ", $title->html;
    

    Replacing stuff is similar:

    $title->html('New Title');
    say "The entirety of google.com with my new title is: ", $page->html;
    

    You can pass an HTML string to the pQuery constructor, which it sounds like you want to do.

    Finally, if you want to use arbitrary HTML as a "template", and then "refine" that with Perl commands, you want to use Template::Refine.

    0 讨论(0)
  • The previous answer is wrong, if the HTML title tag is used more often then this can easily be overcome by checking to make sure that the title tag is valid (no tags in between).

    my ($title) = $test_content =~ m/<title>([a-zA-Z\/][^>]+)<\/title>/si;
    
    0 讨论(0)
  • 2021-01-27 08:35
    use strict;
    use LWP::Simple;
    
    my $url = 'http://www.google.com'|| die "Specify URL on the cmd line";
    my $html = get ($url);
    $html =~ m{<TITLE>(.*?)</TITLE>}gism;
    
    print "$1\n";
    
    0 讨论(0)
  • 2021-01-27 08:52

    HTML::HeadParser does this for you.

    0 讨论(0)
  • 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 <title> and </title> 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 <title> is in one line and </title> is in other line
    
                #starting <title> 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 </title> is found
                    elsif ($_=~/$endstring/i) {
                # read everything before </title> 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 <title> and </title>
                    elsif ($spool == 1) {
                            print WFL " ";
                            print WFL "$_";
    
                            }
    
                        }
            close $fh;
            # end of getting the title name
    
    0 讨论(0)
提交回复
热议问题