473,408 Members | 2,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Need help with some very Practical Extraction

Hi,

What I'm trying to do seems right up Perl's alley, but I can't get it to work. I'm using the WWW::Mechanize module to retrieve a sprawling HTML document from which I want to extract certain strings and save them. I can get this much to work:
Expand|Select|Wrap|Line Numbers
  1. use WWW::Mechanize;
  2. $url = "http://someurl";
  3. my $mechanize = WWW::Mechanize->new(autocheck => 1);
  4. $mechanize->get($url);
  5. my @array_of_data = $mechanize->content;
  6.  
but now I am stuck on how to process that data.

The HTML doc is quite long, and contains numbers that I want to extract, numbers that are always preceeded by a text string that is the same each time, such as:

<a href bla bla bla>bla bla bla<random tag>mydigits=493409834%bla bla bla<meaningless tag>bla bla</a>

where the string "mydigits=" always preceeds the desired number and is sometimes all lowercase but can occasionally look like "MyDigits="; where the number itself may be anywhere from one to 10 digits in length; and where "%" might literally be "%" or any other non-digit character including a space. Moreover, the desired string might appear more than once per line -- assuming Perl doesn't see the HTML doc as just one single long line of text anyway.

What I have tried is many extremely ugly variations on
Expand|Select|Wrap|Line Numbers
  1. my $pattern = "[Mm]y[Dd]igits=[0-9]*[^0-9]";
  2. foreach (@array_of_data){
  3.     if ( /$pattern/ ){
  4.     print "$_\n";
but if I don't get an error, all I get is a spew-out of the entire HTML doc instead of what I am hoping for, which would be a printout or file that looks like:

219824
2230239084
04598
98739874
etc., etc.

or better yet, assign the output to an array that looks like:

@desired_array = ( 219824, 2230239084, 04598, 98739874);

I know I must be missing something very fundamental, so if anyone can help steer me away from the major mistakes I'm making, I'd appreciate it. Thanks.
Jan 28 '07 #1
4 1396
KevinADC
4,059 Expert 2GB
as long as the pattern is on the same line this should work:


Expand|Select|Wrap|Line Numbers
  1. my @digits = ();
  2. foreach (@array_of_data){
  3.     if ( /mydigits=(\d+)/i ){
  4.        print "found $1 in this line: $_\n";
  5.        push @digits,$1;
  6.     }
  7. }
  8. print "$_\n" for @digits;
  9.  

but can be changed if the pattern is broken over multiple lines.
Jan 28 '07 #2
Wow, thank you, that is very helpful. Love the i modifier for case insensitivity!

As I suspected, the HTML doc looks like one long line to Perl, so what happens is it finds the first instance, say, 123456789, prints

"found 123456789 in this line:"

followed by what to you and me looks like more than 3000 lines of HTML, then prints:

"123456789"

and then quits. But that is more than I could get it to do before, and this definitely has me pointed in the right direction, so thanks again. :)
Jan 28 '07 #3
Wow, I just realized what you did with the parentheses and the $1 to extract only the digits. Awesome!
Jan 28 '07 #4
KevinADC
4,059 Expert 2GB
See how this works:

Expand|Select|Wrap|Line Numbers
  1. use WWW::Mechanize;
  2. $url = "http://someurl";
  3. my $mechanize = WWW::Mechanize->new(autocheck => 1);
  4. $mechanize->get($url);
  5. my $string_of_data = $mechanize->content;
  6. my @digits = $string_of_data =~ m/mydigits=(\d+)/igm;
  7. print "$_\n" for @digits;
  8.  
if that doesn't work, change the 'm' after 'ig' to an 's'
Jan 28 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Beach Potato | last post by:
Dear Y'all: I'm about to start porting a big old project written in anscient version of Delphi to something more stable, robust, supportable and maybe even portable. Since I haven't seriously...
44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
38
by: lawrence | last post by:
I'm just now trying to give my site a character encoding of UTF-8. The site has been built in a hodge-podge way over the last 6 years. The validator tells me I've lots of characters that don't...
24
by: Salad | last post by:
Every now and then I see ads that state something like "Experience with Large Databases ...multi-gig...blah-de-blah" And I have to laugh. What's the difference between a large or small database? ...
2
by: ToddLMorgan | last post by:
I'm trying to set any of the dates (create, modification, access) of a directory under windows with python. I'm trying to do this as I'm trying to write a unittest for a directory cleaning...
1
by: Richard Lewis Haggard | last post by:
I need to get up to speed on the new dataset object fast. Can someone recommend a primer or other highly condensed tutorial that will give me an intense injection of practical knowledge on how...
46
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
16
by: EM.Bateman | last post by:
Working on Visual Studio .Net I've implemented a class: #ifndef CONTRIBUTOR_H #define CONTRIBUTOR_H enum Gender {male=1, female, unk}; #include <iostream> #include <iomanip> #include...
5
by: Taras_96 | last post by:
Hi all, Jesse Liberty writes: "cin.get() >>myVarOne >myVarTwo; // illegal The return value of (cin.get() >myVarOne) is an integer, not an iostream object." ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.