By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,809 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,809 IT Pros & Developers. It's quick & easy.

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

P: 5
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.  
Apr 16 '09 #1
Share this Question
Share on Google+
1 Reply


eWish
Expert 100+
P: 971
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
Apr 17 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.