473,978 Members | 26,340 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with some very Practical Extraction

5 New Member
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=493409 834%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 1412
KevinADC
4,059 Recognized Expert Specialist
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
theapeman
5 New Member
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
theapeman
5 New Member
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 Recognized Expert Specialist
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
2668
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 touched C for large implementations, I'm seeking advice on what to use for development. My ultimate goal is to spend as less time on it as possible. I'll be writing it in Windows 32-bit environment, probably Win2000 or Win98. Planning to use a...
44
4781
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 body of the page I put the next code: <BODY onkeydown='notOpenNewWindow();'>
38
2906
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 belong to the UTF-8 encoding. Other than changing them by hand, can anyone think of a clever way to find and convert these characters? http://validator.w3.org/check?uri=http%3A%2F%2Fwww.krubner.com%2F
24
4067
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? A table is a table, a record is a record, a field is a field. All you are doing is manipulating data in tables. I wouldn't think it'd make much difference in working with a table with 10 records or a billion records...they're nothing more than...
2
3399
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 script I'm writing (ie I need the test to set the create/mod time for some of the directories so that I can be sure that the script works properly - as it picks dirs based upon their age). I've tried using the utime( path, times). That doesn't work...
1
1388
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 datasets work? In particular, I know that a dataset can contain multiple tables but can a dataset encapsulate hierarchical data arrangements? The particular application that my company wants me to investigate is if it is practical to use a dataset...
46
2579
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 work involves creating custom packages of our software product for golf courses that purchase our software. The course data is kept as a back up in the event the course needs us to replace their custom files. Each course has a folder of it's own...
16
4376
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 <string>
5
2140
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." (http://newdata.box.sk/bx/c/htm/ch16.htm)
0
10172
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11413
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11581
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7617
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6561
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.