seting cookies to use some links with perl

seting cookies to use some links with perl

am 13.11.2007 17:24:50 von RAPPAZ Francois

Hi
I would like to use a link "Import into textBib" wich is displayed
depending on user's preferences. I tried to set these from a script
before displaying the page but it does not work. am I missing
something obvious ?

Thanks for any help
Francois

My script below:


use LWP;
use HTML::Parser;
use HTML::FormatText;
use HTML::Tree;
use HTML::Form;
# use DateTime::Duration;
use HTTP::Headers;
use HTTP::Cookies;
use HTTP::Cookies::Netscape;
use strict;
use warnings;


#Get the cookies in defining the preferences pages
#save the cookies in a file
#
my $url=3D"http://scholar.google.com/scholar_setprefs";

my $ua =3D LWP::UserAgent->new;
$ua->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:
18.1.9) Gecko/20071025 Firefox/2.0.0.9');
my $c =3D HTTP::Cookies::Netscape->new(file=3D>"cookies.txt",
autosave=3D>"1");

my $h =3D HTTP::Headers->new(
Accept =3D> "text/xml,application/xml,application/xhtml+xml,text/
html;q=3D0.9,text/plain;q=3D0.8,image/png,*/*;q=3D0.5",
Host =3D> "www.unifr.ch",
);
$h->referer("http://www.unifr.ch/dokpe/index.htm");
$h->server("Apache/2.0.46 (Red Hat)");
$h->user_agent("Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:
18.1.9) Gecko/20071025 Firefox/2.0.0.9");

my $req =3DHTTP::Request->new(GET=3D>$url,$h);
# $c->add_cookie_header($req);
$ua->cookie_jar($c);
my $res =3D$ua->request($req);
# $c->add_cookie_header($req);
# print $h->as_string,"\n";
die ($res->status_line) unless ($res->is_success);

# get the form and set the preference to have the bibTex Link
displayed

my @forms =3D HTML::Form->parse($res->content(), $res->base());

# $forms[0]->dump();
# die;

my $box =3D $forms[0]->find_input("scis");
die "pas de box\n" unless $box;
# foreach my $box (@rbox){
#print $box->disable()?"inactif":"actif","\n";
my @v =3D$box->possible_values;
# $box->value( $v[2] );
foreach my $v (@v){print "value : $v\n";}
# $box->value(undef);
$box->value("yes");
#}
my $input =3D $forms[0]->find_input("scisf");
if ($input){
$input->value("4");
} else {die "pas de select\n";}

my $bt =3D $forms[0]->find_input("submit");

$forms[0]->dump;

# send the form
#
if ($bt) {
$res =3D $ua->request($bt->click($forms[0]));
die ($res->status_line) unless ($res->is_success);
} else {print "bt undef\n";}
# die ($res->status_line) unless ($res->is_success);
print $res->status_line,"\n";

# get the cookie back
#
$c->extract_cookies($res);
print $res->base,"\n\n";


$url =3D "http://scholar.google.com/scholar?num=3D1&q=3D%22fribourg,
+switzerland%22+-
author:fribourg&hl=3Den&lr=3D&as_ylo=3D2007&as_yhi=3D2007&as _subj=3Dbio+chm=
+eng
+med+phy&sa=3DN";

$req =3DHTTP::Request->new(GET=3D>$url,$h);

$c->add_cookie_header($req);
# $ua->cookie_jar($c);
#
$res =3D$ua->request($req);
die ($res->status_line) unless ($res->is_success);
my $tree =3D HTML::TreeBuilder->new->parse($res->content);
my @bibtexlinks =3D getbibtex($tree);

foreach my $l (@bibtexlinks){
print "$l\n";

}

my $formatter =3D HTML::FormatText->new();
my $ascii =3D $formatter->format($tree);
print $ascii;


sub getbibtex {
my ($tree) =3D @_;
my @out;
my @links =3D $tree->look_down('_tag'=3D>'a', 'class'=3D>'fl');
foreach my $a (@links){
push @out, $a->attr('href');
# push @out, $a->attr('href');
}
# $tree->delete;
return @out;
}

Script end


The ouput gives for the form (and the relevant prefs seem correct)

GET http://scholar.google.com/scholar_setprefs [prefform]
nosubmit=3D (image)
submit=3DSave Preferences (submit)
inststart=3D0 (hidden readonly)
hl=3Den (option) [...]
lang=3Dall (radio) [*all|some]
lr=3D (checkbox) [*/...]
lr=3D (checkbox) [*/...]
lr=3D (checkbox) [*/...]
lr=3D (checkbox) [*/...]
lr=3D (checkbox) [*/...]
lr=3D (checkbox) [*/...]
lr=3D (checkbox) [*/...]
lr=3D (checkbox) [*/...]
instq=3D (text)
instfind=3DFind Library (submit)
num=3D10 (option) [*10|20|30|50|100]
newwindow=3D (checkbox) [*/off|1/ ...]
scis=3Dyes (radio) [:no|*yes]
scisf=3D4 (option) [*4/BibTeX|3/EndNote|2/
RefMan|1/RefW
orks|5/WenXianWang]
submit=3DSave Preferences (submit)

the status is ok
200 OK
the base url also http://scholar.google.com/schhp?hl=3Den&lr=3D

but the requested link is not display:

/scholar?num=3D1&hl=3Den&lr=3D&cites=3D17696021159454281461
/scholar?num=3D1&hl=3Den&lr=3D&q=3Drelated:9Y4Rq3zllPUJ:scho lar.google.com/
http://www.google.com/search?hl=3Den&lr=3D&q=3D%22STEINGER%2 2+%22Predicting=
+adaptive+e
volution%22

Dump of the result page:

Scholar Home

-

Advanced Scholar Search
Scholar Preferences
Scholar Help

Search only in:

Biology, Life Sciences, and Environmental Science
Chemistry and Materials Science
Engineering, Computer Science, and Mathematics
Medicine, Pharmacology, and Veterinary Science
Physics, Astronomy, and Planetary Science

Search in all subject areas.


Scholar All articles - Recent articles

Results 1 - 1 of about 290 for "fribourg, switzerland"
-author:fribourg. (0.08 seconds)


All Results

A Dulloo

J Montani

A Felikson

P Lavenex

U Albrecht

Predicting adaptive evolution under elevated atmospheric CO2 in the
perennial grass Bromus erectus
T STEINGER, A STEPHAN, B SCHMID - Global Change Biology, 2007 -
ingentaconnect.com
... 1 Present Address: D├Â=AEpartement de Biologie, Unitâ”=
œÂ=AE Ecologie &
Evolution, Universit├Â=AE
de Fribourg, Chemin du Mus├Â=AEe 10, CH-1700 Fribourg,
Switzerland. ...
Cited by 1 - Related Articles - Web Search