With LWP; wish to print a part of a page in browser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dibyenduchatterjee
    New Member
    • Apr 2009
    • 5

    With LWP; wish to print a part of a page in browser

    Am using LWP module.
    I wish to read a page and print a part of it.
    I want t read first 300 characters of <div id="bodyContent "> written inside the html page.

    Code I am using :
    Code:
    #!/usr/bin/perl
    
    use DBI;
    use CGI;
    use LWP::Simple;
    use LWP::UserAgent;
    use DateTime::Format::Strptime;
    use JSON;
    use HTML::Template;
    use HTML::Parser;
    use HTML::Entities;
    
    
    $q = new CGI;
    print "Content-type: text/html\n\n";
    
    my $url = '------site url comes here ----';
    
      use LWP::Simple;
      my $content = get $url;
      die "Couldn't get $url" unless defined $content;
    
      #$description = &convert_to_page(substr($content,0,650)); 
      print $content;
    
    1;
    Last edited by eWish; Apr 16 '09, 11:49 PM. Reason: Please use the code tags
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    What part are you having problems with? Also, since you are using the CGI.pm have it print your header for you.
    Code:
    print $q->header();
    instead of:
    Code:
    print "Content-type: text/html\n\n";
    You only have to have this once in your script for it to work. So, I would suggest that your remove the second one you have listed.
    Code:
    use LWP::Simple;
    I can't see what you are doing in this sub "&convert_to_pa ge" but, you can use substr to get the first 300 characters by changing the 650 to 300.
    Code:
    my $page = substr($content,0,300);
    print $page;
    You could also use a regex to if you wish.

    --Kevin

    Comment

    Working...