Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: A regex question

by ww (Archbishop)
on Oct 29, 2011 at 04:40 UTC ( #934556=note: print w/replies, xml ) Need Help??

in reply to A regex question

roboticus has shown how to deal with the multi-line para problem, so I'm omitting that. But as the AM above suggests, using an appropriate module or modules can save you much grief.

What follows is NOT code to adopt or emulate; rather it is intended to suggest the pitfalls you may encounter in dealing the html. Web pages may be fully compliant, well-structured (for readability and maintenance); and reasonably consistent within a <ol>, <ul>, or <table> (and those are just "fer'instances") but don't bet the farm on that!

That which you're scraping may well be less than well-formed, compliant or consistent. And that makes parsing difficult. This bad example confines itself to some minor variance and only in the rendered portion of a link:

#!/usr/bin/perl use Modern::Perl; # 934485 my ($name, @names, @scrapings, $scraping); while ( <DATA> ) { $scraping = $_; push @scrapings, $scraping; } for $scraping(@scrapings) { $scraping =~ /(\d\w+ # begin capture, num followed by 1 or more +wordchars [. ]* # charclass of dot or space (?: # non-capture grouping \w+ # wordchars [. ]+ # one or more of a literal dot or + space )* # group is OPTIONAL by quantifyin +g it as "0 or more" [\w-]+ # wordchars or hyphens \s* # zero or more spaces ) # end capture before <\/a> # close link_renderable and href /x; # extended format if ($1) { $name = $1; push @names, $name; } } for $name (@names) { say $name; } say "\n\t Done"; __DATA__ <li><a href="page.aspx?1ar3xlr29">1Mary Mary QuiteContrary </a></li> <li><a href="page.aspx?43xlr17">2Sam Samuels</a></li> <li><a href="page.aspx?3719rlr17qt">3Joe.Bones</a></li> <li><a href="page.aspx?a=c4bc46eswsdw32fcc">4John.Martines </a></li> <li><a href="page.aspx?a=0a2b-a99d-3754eb2f5e35">5Mary Jones</a></li> <li><a href="page.aspx?a=1ef7b100-8dc4-4b40-871c-68b1d0">6Fernando Pra +deras</a></li> <li><a href="page.aspx?a=e8ec1d77-ee83-4797-b9c4-7676053a4926"> 7blifs +tik</a></li> <li>foobar baz blivitz <a href="page.aspx?a=e8ec1d77" class="b">8Frede +rick B. Ohlmsted</a> more blah blah blah and so on ad nauseum....</l +i> <li><a href="page.aspx?a=c4bc46eswsdw32fcc">9Ernesto Maria Santiago-Co +rtez</a></li>

Executing that script produces what I believe to be data consistent with your spec:

1Mary Mary QuiteContrary 2Sam Samuels 3Joe.Bones 4John.Martines 5Mary Jones 6Fernando Praderas 7blifstik 8Frederick B. Ohlmsted 9Ernesto Maria Santiago-Cortez Done

But look at the hoops we jumped thru to get here -- for what are fundamentally small-potatoes differences in the style of the names. Strong Suggestion: Don't do it this way.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://934556]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-02-04 02:20 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (30 votes). Check out past polls.