Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Searching for a string in outfile

by ysksai (Initiate)
on May 07, 2009 at 21:13 UTC ( [id://762701]=perlquestion: print w/replies, xml ) Need Help??

ysksai has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a question, I am trying to use WWW::Mechanize to login to yahoo mail and get the contents into outfile. here is the script: my $mech = WWW::Mechanize->new(); $mech->get($url); $mech->form_name('loginform'); $mech->field(login => $username); $mech->field(passwd => $password); $mech->click(); my $outpage = $mech->content(); open(OUTFILE, ">$outfile"); print OUTFILE "$outpage"; at this point it gives me the contents of the very first page in yahoo mail. now Here is my question: I want to search for the string for how many emails do I have from the outfile and store it in another file and print the final file which has information on how many emails I have. like for example when I open my final file it should have " You have 5 unread messages:" any sugegstions??? appreciate your help. Thanks

Replies are listed 'Best First'.
Re: Searching for a string in outfile
by Corion (Patriarch) on May 07, 2009 at 21:16 UTC
      Thanks but Still having a tough time.. anyone has a sample code that can help me? appreciate it!
Re: Searching for a string in outfile
by graff (Chancellor) on May 08, 2009 at 01:19 UTC
    Given that you have this code already (which would be displayed in your post as follows, if you just put <code>...</code> tags around the code):
    my $mech = WWW::Mechanize->new(); $mech->get($url); $mech->form_name('loginform'); $mech->field(login => $username); $mech->field(passwd => $password); $mech->click(); my $outpage = $mech->content(); open(OUTFILE, ">$outfile"); print OUTFILE "$outpage";
    If it's true that the contents of "$outpage" include a string that matches this regular expression:
    /You have \d+ unread messages/
    Then you just need four more statements: match and capture that string in $outpage, open your "final" output file, write the captured string to the output file, and close the file. If you need to see the code for that...
    if ( $outpage =~ /(You have \d+ unread messages)/ ) { open( FINAL, ">", "make_up_a_file_name" ) or die $!; print FINAL $1,"\n"; close FINAL; }
    If that regex doesn't match, you'll need to check the actual contents of "$outpage", and figure out how the regex would need to change in order to match the desired phrase.
      Thank you so much it works!
        Hi Again, I am trying to use the same script on my prod server link with userid/password for it and I am receiving the following error: Error GETing http://"server link:" Unauthorized at script.pl line 11 ( line 11 is $mech->get($url);) Please advice. Thanks in advance,
        Great. Just for grins, how about you update your OP and put in the code tags (and include a little update note to make it clear that you changed the post).

        As for this error that you apparently reported (posting as Anonymous Monk):

        GETing http://"server link:" Unauthorized at script.pl line 11
        Are you sure that the userid/password in your script are exactly the ones you are supposed to use? (Are you sure the url string is correct?) Do those exact strings work when you paste them into an interactive (manual) connection?

        Check the WWW::Mechanize docs about setting the "user agent" that is reported in the request that you send -- the server might be using that item to limit misuse of their service. (I'm just guessing -- I don't know much about this part of the process.)

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://762701]
Approved by ELISHEVA
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2024-04-26 07:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found