CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
how to read fieldname from a differnt HTML file

 



msing005
Novice

Feb 14, 2008, 8:57 AM

Post #1 of 18 (2848 views)
how to read fieldname from a differnt HTML file Can't Post

I am trying to read two fields from other html file but I dont know how to do it....I am posting my code here....it will be great if any one of you guys can hel me:)
#!c:/perl/bin/perl
use CGI ':standard';#including CGI.pm class
$newsVariable = new CGI; #creating new object from CGI.pm class
#opening Directory
opendir(DIR,"./");
@files=grep(/^item/,readdir(DIR));
close(DIR);

#Starting html
print header();
print $newsVariable->start_html(-title=>"News");
#title of the page
print $newsVariable->center,h1("HEADLINES \n");

print $newsVariable-> start_table({-width=>400,-border=>'1'}),"\n\n";#creating table
print $newsVariable-> start_Tr,$newsVariable->start_th,"Date",$newsVariable->end_th;
print $newsVariable->start_th,"Abstract",$newsVariable->end_th,$newsVariable->end_Tr;
for $file(param(@files)){

print $newsVariable->start_Tr;
#print "file name:$file";
open(ITEM,"./$file");# opening the item file
$title=<ITEM>;
print $newsVariable->start_form("./$file");# starting form
for $news($title)
{

print $newsVariable->start_th;
$date = em(param('rel_date'));
print $date,$newsVariable->end_th;
print $newsVariable->start_th;
$abstract = em(param('abstract'));
print $abstract,"\n",$newsVariable->a({-href=>"./$file"},"More>>>"),$newsVariable->end_th;
}
close ITEM;
print$newsVariable->end_form;
print $newsVariable->end_Tr;
}
print$newsVariable->end_table;

print $newsVariable->end_html();


KevinR
Veteran


Feb 14, 2008, 10:29 AM

Post #2 of 18 (2846 views)
Re: [msing005] how to read fieldname from a differnt HTML file [In reply to] Can't Post

ask a question that can be answered and I will try and help. Your current question is too vague, describe what you are trying to do in more detail and describe where you are having trouble.
-------------------------------------------------


msing005
Novice

Feb 14, 2008, 12:37 PM

Post #3 of 18 (2845 views)
Re: [KevinR] how to read fieldname from a differnt HTML file [In reply to] Can't Post

#!c:/perl/bin/perl
use CGI ':standard';#including CGI.pm class
$newsVariable = new CGI; #creating new object from CGI.pm class
#opening Directory
opendir(DIR,"./");
@files=grep(/^item/,readdir(DIR));
close(DIR);

#Starting html
print header();
print $newsVariable->start_html(-title=>"News");
#title of the page
print $newsVariable->center,h1("HEADLINES \n");

print $newsVariable-> start_table({-width=>400,-border=>'1'}),"\n\n";#creating table
print $newsVariable-> start_Tr,$newsVariable->start_th,"Date",$newsVariable->end_th;
print $newsVariable->start_th,"Abstract",$newsVariable->end_th,$newsVariable->end_Tr;
for $file(param(@files)){

print $newsVariable->start_Tr;
#print "file name:$file";
open(ITEM,"./$file");# opening the item file
$title=<ITEM>;
print $newsVariable->start_form("./$file");# starting form
for $news($title)
{

print $newsVariable->start_th;
*$date = em(param('rel_date'));
print $date,$newsVariable->end_th;
print $newsVariable->start_th;
*$abstract = em(param('abstract'));
print $abstract,"\n",$newsVariable->a({-href=>"./$file"},"More>>>"),$newsVariable->end_th;
}
close ITEM;
print$newsVariable->end_form;
print $newsVariable->end_Tr;
}
print$newsVariable->end_table;

print $newsVariable->end_html();

In the above problem I am trying to read the contents of the directory and evey file in the directory starts with itemX.html, Where X is a number for example 2,3,4 or 5 . this itemX.html file has two field names rel_date and abstract. I am trying to print those two things in the table. the * sign is the place where I am trying to store value in variables.
I hope it clear now if not please do let me know.
Thanks


KevinR
Veteran


Feb 14, 2008, 1:19 PM

Post #4 of 18 (2843 views)
Re: [KevinR] how to read fieldname from a differnt HTML file [In reply to] Can't Post


Quote
this itemX.html file has two field names rel_date and abstract


show an example of how the data in itemX.html files is structured, explain if it is not clear.
-------------------------------------------------


KevinR
Veteran


Feb 14, 2008, 5:45 PM

Post #5 of 18 (2841 views)
Re: [msing005] how to read fieldname from a differnt HTML file [In reply to] Can't Post

I think maybe you are confused about something:

*$date = em(param('rel_date'));
print $date,$newsVariable->end_th;
print $newsVariable->start_th;
*$abstract = em(param('abstract'));

param('rel_date') and parm('abstract') would be data sent to your script from a form, not read in from a file. I think you can read in data from a file but I don't think you use param() to get that data. I am not positive about that, I would have to read the CGI module documentation.
-------------------------------------------------


msing005
Novice

Feb 15, 2008, 6:02 AM

Post #6 of 18 (2839 views)
Re: [KevinR] how to read fieldname from a differnt HTML file [In reply to] Can't Post

Thats makes sense....So how can I parse the html file...and here I am specifically looking for "contents" of Meta tag from the html file.


KevinR
Veteran


Feb 15, 2008, 9:26 AM

Post #7 of 18 (2837 views)
Re: [msing005] how to read fieldname from a differnt HTML file [In reply to] Can't Post


In Reply To
Thats makes sense....So how can I parse the html file...and here I am specifically looking for "contents" of Meta tag from the html file.


Are the html files all structured the same or is each different? You mentioned two fields: date and abstract, what does that relate to in the html documents you want to parse?
-------------------------------------------------


msing005
Novice

Feb 15, 2008, 9:33 AM

Post #8 of 18 (2836 views)
Re: [KevinR] how to read fieldname from a differnt HTML file [In reply to] Can't Post

yes they are structured in same well.....I was looking through the HTML::Parser packaage....but i didnt know much about it.

is there any CGI package which we can use


KevinR
Veteran


Feb 15, 2008, 2:26 PM

Post #9 of 18 (2834 views)
Re: [msing005] how to read fieldname from a differnt HTML file [In reply to] Can't Post

I never use HTML::Parser, and it may be overkill for what you want to do, but so far you have not answered my questions so until you do I can't really help anymore.


Quote
show an example of how the data in itemX.html files is structured, explain if it is not clear.



Quote
You mentioned two fields: date and abstract, what does that relate to in the html documents you want to parse?

-------------------------------------------------


msing005
Novice

Feb 15, 2008, 2:40 PM

Post #10 of 18 (2833 views)
Re: [KevinR] how to read fieldname from a differnt HTML file [In reply to] Can't Post

the itemX.html file is like any other html file
it has meta tags :

<meta name="date" content = "xxxx-xx-xx">
<meta name="abstract" content=" asssa asssa asasasa assad">

I tried an approach but it is not very efficient.

tell me if this helps


KevinR
Veteran


Feb 15, 2008, 3:26 PM

Post #11 of 18 (2832 views)
Re: [msing005] how to read fieldname from a differnt HTML file [In reply to] Can't Post

The code you posted earlier is a little confusing, but see how well this works, I can't test it so I would be surprised if it did not need some tweaking to get working with your files:


Code
#!c:/perl/bin/perl 
use strict;
use CGI::Carp qw/fatalsToBrowser/;
use CGI;
my $newsVariable = CGI->new; #creating new object from CGI.pm class
#opening Directory
opendir(DIR,"./") or die "Can't opendir: $!";
my @files = grep {/^item/} readdir(DIR);
close(DIR);

#Starting html
print $newsVariable->header(),
$newsVariable->start_html(-title=>"News"),
$newsVariable->h1("HEADLINES"),
$newsVariable->start_table({-width=>400,-border=>'1'}),"\n\n",
$newsVariable->start_Tr,$newsVariable->start_th,"Date",$newsVariable->end_th,
$newsVariable->start_th,"Abstract",$newsVariable->end_th,$newsVariable->end_Tr;

foreach my $file ( @files ){
print $newsVariable->start_Tr;
open(ITEM,"./$file");# opening the item file
my ($date, $abstract);
while (<ITEM>) {
if (/<meta\s+name=["']?rel_date["']?\s+content=["']?([^>]+)>/i) {
$date = $1;
$date =~ s/["']$//;
}
elsif (/<meta\s+name=["']?abstract["']?\s+content=["']?([^>]+)>/i) {
$abstract = $1;
$abstract =~ s/["']$//;
}
if ($date && $abstract) {
print $newsVariable->start_th,
$date,
$newsVariable->end_th,
$newsVariable->start_th,
$abstract,
$newsVariable->a({-href=>"./$file"},"More>>>"),
$newsVariable->end_th,
$newsVariable->end_Tr;
last;
close ITEM;
}
else {
next;
}
}
}
print $newsVariable->end_table,
$newsVariable->end_html();

-------------------------------------------------


msing005
Novice

Feb 15, 2008, 5:05 PM

Post #12 of 18 (2831 views)
Re: [KevinR] how to read fieldname from a differnt HTML file [In reply to] Can't Post

Cool
yes it works thank you


KevinR
Veteran


Feb 15, 2008, 7:35 PM

Post #13 of 18 (2829 views)
Re: [msing005] how to read fieldname from a differnt HTML file [In reply to] Can't Post

You're welcome
-------------------------------------------------


msing005
Novice

Feb 17, 2008, 7:35 PM

Post #14 of 18 (2825 views)
Re: [KevinR] how to read fieldname from a differnt HTML file [In reply to] Can't Post

can you tell me a good web tutorial which can help me learning perl better.


KevinR
Veteran


Feb 18, 2008, 12:56 PM

Post #15 of 18 (2823 views)
Re: [msing005] how to read fieldname from a differnt HTML file [In reply to] Can't Post

here is a link to some free online perl books:

http://www.perl.org/books/library.html

You maybe want to concentrate on Beginning Perl.

Go down to the "Tutorial Section" of this page:

http://www.perlmonks.com/index.pl?node=Tutorials
-------------------------------------------------


msing005
Novice

Feb 19, 2008, 5:54 AM

Post #16 of 18 (2815 views)
Re: [KevinR] how to read fieldname from a differnt HTML file [In reply to] Can't Post

can you explain this part...I have hard time understanding this:
if (/<meta\s+name=["']?rel_date["']?\s+content=["']?([^>]+)>/i) {
$date = $1;
$date =~ s/["']$//;


KevinR
Veteran


Feb 19, 2008, 8:45 AM

Post #17 of 18 (2813 views)
Re: [msing005] how to read fieldname from a differnt HTML file [In reply to] Can't Post


In Reply To
can you explain this part...I have hard time understanding this:
if (/<meta\s+name=["']?rel_date["']?\s+content=["']?([^>]+)>/i) {
$date = $1;
$date =~ s/["']$//;


well, there is a lot going on in that little bit of code. In a regular expressions chapter/tutorial look up:

a character class: []
a negated character class: [^]
quantifiers: ? + * {n,n}
capturing group: () $1
short-cut character class: \s \d \w (etc)

and you should get a better understanding of whats going on.
-------------------------------------------------


KevinR
Veteran


Feb 25, 2008, 5:00 PM

Post #18 of 18 (2792 views)
Re: [msing005] how to read fieldname from a differnt HTML file [In reply to] Can't Post


In Reply To
can you explain this part...I have hard time understanding this:
if (/<meta\s+name=["']?rel_date["']?\s+content=["']?([^>]+)>/i) {
$date = $1;
$date =~ s/["']$//;


Did you read up on regular expressions and the things I pointed out to you?
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives