Click to See Complete Forum and Search --> : tell-a-friend script and grabbing the title of a html-page


hooba
01-10-2003, 04:30 PM
I've done some small changes to a tell-a-friend script (http://folk.uio.no/leikh/nyheter/script/mailpage.pl ).

First i took into account the help from javascript to read the url of a page in a second frame, and put this in a hidden form field, which then is read by the cgi-script.
(this form is here: http://folk.uio.no/leikh/nyheter/tipsnyhet.html )

(the actual stuff is at http://folk.uio.no/leikh/nyheter )


I then wanted the title of the html-page (which is sent in the body of the mail) to be printed in the subject field of the mail.

I added a use HTML::TokeParser; etc.....


I get no error-messages, but i don't get the title in the subject either...


But when i write the url directly into the $p = HTML::TokeParser->new(shift||"$FORM{'url'}"); , it works....

i.e: $p = HTML::TokeParser->new(shift||"http://folk.uio.no/leikh/nyheter/www.uio.no/offentlig/news/messages/1100368.html");



Anyone see what's wrong?

jeffmott
01-10-2003, 10:59 PM
You aren't actually extracting any tokens yet.
HTML::TokeParser (http://www.perldoc.com/perl5.8.0/lib/HTML/TokeParser.html)

$p->get_tag( [$tag, ...] )

This method returns the next start or end tag (skipping any other tokens), or undef if there are no more tags in the document. If one or more arguments are given, then we skip tokens until one of the specified tag types is found. For example:

  $p->get_tag("font", "/font");

will find the next start or end tag for a font-element.

The tag information is returned as an array reference in the same form as for $p->get_token above, but the type code (first element) is missing. A start tag will be returned like this:

  [$tag, $attr, $attrseq, $text]

The tagname of end tags are prefixed with "/", i.e. end tag is returned like this:

  ["/$tag", $text]

hooba
01-11-2003, 05:25 AM
hmmm.....
did you bother to take at look at the script?
http://folk.uio.no/leikh/nyheter/script/mailpage.pl

hooba
01-11-2003, 06:42 AM
well, some one just told me that

"HTML::TokeParser->new() takes either a file name, file handle or a document. not a url"

and that

"you would presumably have to add the code to turn a url into html. e.g. with LWP::Simple"


But how?

I'm not skilled ;)
But i'm learing...

jeffmott
01-11-2003, 09:06 AM
Sorry. I do see the problem now though.
use HTML::TokeParser;
$p = HTML::TokeParser->new(shift||"$FORM{'url'}");
if ($p->get_tag("title")) {
my $title = $p->get_trimmed_text;
}
You my() declare $title lexically scoped to inside the if block.

my $title;
if ($p->get_tag('title')) {
  $title = $p->get_trimmed_text();
}

And getting HTML

use LWP::Simple;
my $content = get('uri');
if (!defined $content) {
  die 'Couldn't get it!';
}

hooba
01-11-2003, 10:26 AM
i'm not following you...

how is introducing $content without anything else gonna help me?

and you write get('uri') . is this as in URI::URL?

tried get('url'), but no...

well, anyways - it's not working... not even after removing
your single quote in "couldn't" ;)

jeffmott
01-11-2003, 08:53 PM
hooba
how is introducing $content without anything else gonna help me?
HTML::TokeParser will not accept a URI because it does not perform an HTTP request by itself. So we use LWP::Simple for the request, loaded into $content, and then provide a reference to that scalar for HTML::TokeParser.

use HTML::TokeParser;
use LWP::Simple;

my $content = get('http://www.w3.org/');
my $p = HTML::TokeParser->new(\$content);

my $title;
if ($p->get_tag('title')) {
  $title = $p->get_trimmed_text();
}

hooba
tried get('url'), but no...
That was an example, you do need to replace uri with an actual URI.

get('http://www.w3.org/');

hooba
not even after removing your single quote in "couldn't"
oops :rolleyes:

hooba
01-12-2003, 05:34 AM
it's just that i don't know the url, and this is the essence of the problem... --> the url is posted by the form...

hooba
01-12-2003, 06:14 AM
this is what i've accomplished now
http://folk.uio.no/leikh/nyheter/script/mailpage.pl

but it's still not working...

to test it, just go to http://folk.uio.no/leikh/nyheter
and follow the 'tips en venn' in upper frame...

If you fill out the form correctly, you'll get 'Ditt tips er sendt til:' :)


/LH

jeffmott
01-12-2003, 10:59 AM
First problem is that you are setting the value of $content after you use it for HTML::TokeParser. Second problem is that $title is still lexically scoped to only the if block.

hooba
01-12-2003, 12:35 PM
well, i've put it here and there and everywhere, but alli end up with is just getting even more confused...
now it's more messy than ever...

i give up.

anyways, thx for your help :)