Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Apr 2009
Posts: 5
#1: Apr 16 '09
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 :
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use DBI;
  4. use CGI;
  5. use LWP::Simple;
  6. use LWP::UserAgent;
  7. use DateTime::Format::Strptime;
  8. use JSON;
  9. use HTML::Template;
  10. use HTML::Parser;
  11. use HTML::Entities;
  12.  
  13.  
  14. $q = new CGI;
  15. print "Content-type: text/html\n\n";
  16.  
  17. my $url = '------site url comes here ----';
  18.  
  19.   use LWP::Simple;
  20.   my $content = get $url;
  21.   die "Couldn't get $url" unless defined $content;
  22.  
  23.   #$description = &convert_to_page(substr($content,0,650)); 
  24.   print $content;
  25.  
  26. 1;
  27.  

eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#2: Apr 17 '09

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


What part are you having problems with? Also, since you are using the CGI.pm have it print your header for you.
Expand|Select|Wrap|Line Numbers
  1. print $q->header();
instead of:
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1. use LWP::Simple;
I can't see what you are doing in this sub "&convert_to_page" but, you can use substr to get the first 300 characters by changing the 650 to 300.
Expand|Select|Wrap|Line Numbers
  1. my $page = substr($content,0,300);
  2. print $page;
You could also use a regex to if you wish.

--Kevin
Reply

Tags
chatterjee, dibyendu, dibyendu chatterjee, lwp, perl