Solved

Generating Webpage dynamically with Perl

Posted on 2004-05-02
11
240 Views
Last Modified: 2013-12-25
I believe this question is really simple.  I am have a Perl CGI file that I read a page through and I need to be able to then print out the html webpage.  I am in essence creating an html frame without frames.  So when you click on a link it passes you through this page and adds some stuff at the top.  I am fine when the page is part of my website and everything is local because I can read the file into an array and then print it out.  The problem is that a couple of the links are on another site so they come in as the full path with http://etc.  
I need to have a way of knowing the url to just print out the html.  Is there a command or a simple way of doing this.
0
Comment
Question by:spoma
11 Comments
 

Author Comment

by:spoma
ID: 10973948
I found LWP::Simple and this seems to do what I needed.  
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10973953
To output the contents of a URL, do:

use LWP::Simple;
getprint('http://www.example.com/page.html');
0
 

Author Comment

by:spoma
ID: 10991951
TinTin
That worked to a degree.  The problems I encountered was that it was including all of the html heading tags.  I am trying to use a component called HTML::Parser and I have it pretty much working but I am trying ot get it not only to print out text but also to print out the <tags>.  here is th sample code from the web i found.  anyone have any ideas how to make this print out tags also.


 use HTML::Parser ();

  sub start_handler
  {
    return if shift ne "title";
    my $self = shift;
    $self->handler(text => sub { print shift }, "dtext");
    $self->handler(end  => sub { shift->eof if shift eq "title"; },
                           "tagname,self");
  }

  my $p = HTML::Parser->new(api_version => 3);
  $p->handler( start => \&start_handler, "tagname,self");
  $p->parse_file(shift || die) || die $!;
  print "\n";
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10992657
Try

use LWP::Simple;
use CGI ':standard';

$page = get("http://www.example.com/page.html");
print escapeHTML($page);
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:spoma
ID: 10995158
That does not seem to work either.  What I need is that I do a get like what you suggest:

$treeview = get(url);

this will then get the entire contents of this url.  i need to be able to then strip out certain tags such as <html>,<title>asdfsaDF</title>, any meta information, the <head> tags so that my html will be valid.  

I am in essence taking this html and putting it in a table in the center of my webpage.  so i must get rid of all tags that will make it invalid html.  Am I going about this the best way?  The example I give gives me the ability to pull out certain tags but I can not get it to print the tags I want it merely prints the text associated with the tags.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11000090
So you're asking for something different now.  Previously you said you wanted to print out the tags also.

What form is the data in the page you are retreiving?  Is it already in a table, or is is just straight paragraphs?

0
 

Author Comment

by:spoma
ID: 11000443
No.  I am asking for the same thing.  The current code sample I showed you prints out only the text between certain <TAGS>.  I need it to print out the <TAGS> along with it.  So if I have HTML like the following

<html>
<head>
<style>hello</style>
</head>
etc....

And I say to print out between the <head> tags i need it to give me
<style>hello</style>
currently it would give me
hello
0
 

Accepted Solution

by:
gorch earned 50 total points
ID: 11013773
Hi spoma,

as far as I understand your problem you will need something like:

  # Read in the url line-wise as you would read a file.
  foreach $line (<url>) {

    # Check whether you reached your pattern from which you want to start collecting content.
    if ($line =~ /<start-tag-or-pattern>/) {

        # From now on until the end-tag appears do what you want with the content.
        do {
            print"$line";
        } until ($line =~/<end-tag-or-pattern/);

    }

  }

Embedding this peace of code in a loop and making the patterns variable will enable you to fetch various content pieces from whithin the page.

Does this help?

gorch
0
 
LVL 6

Assisted Solution

by:nitinsawhney
nitinsawhney earned 50 total points
ID: 11144700
Hi Spoma,
One more if you want to print the html code rather then allowing browser to render it then try something like this.


do{
    $line =~ s/</&lt;/g;  
    $line =~ s/>/&gt;/g;
    print "$line";
   }until ($line =~/<end-tag-or-pattern/);


--
Nitin
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now