PDA

View Full Version : Opening Web Page and Grabbing Text


wpskibum
07-12-2005, 10:43 PM
I want to write a perl script that I can run that will open a web page grab some of the text off of it and then print it out to the comand prompt window where I ran the script from. I have a few things I want to use this for and all of them the website has the text in the same location all the time. As an example I am looking for a script that I can run that will grab the temperature off of a web site and print it back to me without me having to go to the site. Also looking to grab information on how the stock market did that day. Is the DOW up or down and how much. If any one could help me with this or tell me if a task like this is even possible in Perl I would appricate the help. Thanks in advance for all your time.

Aradon
07-12-2005, 11:03 PM
I believe you'll be wanting to look in the LWP modules then.

On this: http://www.perl.com/pub/a/2002/08/20/perlandlwp.html website they have a simple example


my $url = 'http://freshair.npr.org/dayFA.cfm?todayDate=current';
# Just an example: the URL for the most recent /Fresh Air/ show

use LWP::Simple;
my $content = get $url;
die "Couldn't get $url" unless defined $content;

# Then go do things with $content, like this:

if($content =~ m/jazz/i) {
print "They're talking about jazz today on Fresh Air!\n";
} else {
print "Fresh Air is apparently jazzless today.\n";
}


Let us know what you come up with and if it works :)

wpskibum
07-13-2005, 01:28 AM
Thank you so much I have a good start on what I am doing. Now I would like to get some help on just grabbing text. For example on http://finance.yahoo.com/ in the upper left hand corner it has table with the DOW and NYSE and the volume of each as well as the gain or loss for the day. Any hints or sections to look at to help me. I want to print these out when I run my script to the screen just saying that the DOW is at a volume of this and dropped this much today. Any help on how to grab these specific values would be great.
Thanks for the help Aradon I was able to do a lot with that information.

wpskibum
07-15-2005, 09:33 PM
I want to thank everyone for there hints and direction to look in. He is the script I have come up with if anyone would find it useful. It goes out to the yahoo finance page and prints to the screen The Dow, NYSE and S&P numbers. Hope someone else can get some use out of it or can modify it for another use. Let me know if you use it and what for and how it works for you. Not that I care who is using it I would just like to know what others think to do along these lines. Thanks for the help and here is the code I am sure it could be cleaned up sorry I didn't clean it up.

my $url = 'http://finance.yahoo.com/';
use LWP::Simple;
my $content = get $url or die "Couldn't get $url" unless defined $content;

#Split HTML into Array on New Lines
@lines = split '\n', $content;

my $line;
my $digit;
my $count;
foreach $line (@lines)
{
$count = 0;
$digit='';
#Only deal with lines with Dow in them
if($line =~ m/Dow/)
{

@tags = split '>',$line;

foreach $tag (@tags)
{
$count = $count + 1;
#Find Market and save Symbol
if($tag =~ m/(Dow)/ || $tag =~ m/(Nasdaq)/ || $tag =~ m/(S\&amp)/)
{
#Save Proper Symbol
$trade = $1.$2.$3;
$count = $count + 2;
#Line Containing Dollar Amount
$moneyline=$tags[$count];

if($moneyline =~ /(\d{1,2}),(\d{3})\.(\d{2})/)
{
#Print Market and Value
print "$trade\t$1,$2.$3";

$count = $count + 3;
$gainloss = $tags[$count];

if($gainloss =~ /(\+|-)(\d{1,3})\.(\d{2})/)
{
print " $1$2.$3\n";
}
$count = $count - 3;
}
$count = $count - 2;
}
}
print "\n";
}
}

Aradon
07-18-2005, 05:30 PM
Just remember, it's always good to comment your code when you start placing it in the real world applications..

That way when the script has to be updated, it will take a coder less time to figure out what your code is doing ;)

netroact
07-19-2005, 07:34 AM
Hey thanks! That looks like a script I might use for something.