Re:Extracting data from Wikipedia using curl, grep, cut and ...
by jpatokal on Tuesday August 16, 2016 @12:05AM
(#12295467)
I used to work with Wiki markup for a living. The time you think you'll save with regex hackery is quickly chewed up by the time wasted eternally tweaking your regexes to catch yet another corner case -- it's much better just to parse for real from the get go, just like it's much better to use a real HTML/XML parser than trying to do the same job badly with regexes.
Re:Extracting data from Wikipedia using curl, grep, cut and ...
by ivanhoe on Tuesday August 16, 2016 @05:31AM
(#12296348)
I used to make scrappers for living and trust me that it all depends on the particular situation and your requirements. Real HTML parsers are easier and safer for general type of work, but they quickly get very heavy on memory when parsing big DOM trees. If all you need from a page is a few strings, like e.g. just a product price (very common task), using regexes is far superior approach performance-wise. It's both faster and uses less memory (so you can run more parallel workers) and also if you write it well it's immune to many small html/design changes as long as the pattern you look for is not changed.
Re:Extracting data from Wikipedia using curl, grep, cut and ...
by jpatokal on Tuesday August 16, 2016 @07:12AM
(#12296643)
I'm sure that works well on product pages, which are just the same template reused for every single product. I'm afraid it will fail spectacularly on wiki pages, which are handcrafted by humans with all the completely unpredictable randomness that entails.
Re:Extracting data from Wikipedia using curl, grep, cut and ...
by elmigranto on Tuesday August 16, 2016 @12:29AM
(#12295543)
I mostly agree, been using Regexes to parse known limited subset of HTML tags in known limited format (1 tag per line, forced padding, no attrs, etc.).
But on the other hand, this is often how long term "proper" solutions are born — evolved from something cobbled together in couple of hours.
Re:Extracting data from Wikipedia using curl, grep, cut and ...
by taneq on Tuesday August 16, 2016 @01:11AM
(#12295661)
Oh yeah, the golden rule for quick hacks is NEVER show them to someone non-technical, especially someone non-technical who's in your chain of command.
I learned that one after mocking up some UI screens using VB6 and then having to explain over and over again that no, just because we showed you some buttons on a page doesn't mean that the program (which had to be a Java applet, mind you) was "almost done."
Re:Extracting data from Wikipedia using curl, grep, cut and ...
by jpatokal on Tuesday August 16, 2016 @03:03AM
(#12295982)
There are already literally dozens of "proper" MediaWiki parsers out there: https://www.mediawiki.org/wiki/Alternative_parsers
Parsing HTML with regexps is fine if you're just curious roughly how many images are in a page. It's great for quick command line experiments. It's just not good when you need to be "doing it properly".