TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Extracting data from Wikipedia using curl, grep, cut and other bash commands

218 pointsby loigealmost 9 years ago

21 comments

lacksconfidencealmost 9 years ago
Because i was randomly curious, can extract this data from the structured html with some dom selectors in a similarly haphazard way:<p>Start with: <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;api&#x2F;rest_v1&#x2F;page&#x2F;html&#x2F;List_of_Olympic_medalists_in_judo" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;api&#x2F;rest_v1&#x2F;page&#x2F;html&#x2F;List_of_Olymp...</a><p>Run this js one-liner: [].slice.call(document.querySelectorAll(&#x27;table[typeof=&quot;mw:Transclusion mw:ExpandedAttrs&quot;] tr td:nth-child(n+2) &gt; a:nth-child(1), table[typeof=&quot;mw:Transclusion mw:ExpandedAttrs&quot;] tr:nth-child(3) td &gt; a:nth-child(1)&#x27;)).map(function(e) { return e.innerText; }).reduce(function(res,el) { res[el] = res[el] ? res[el] + 1 : 1; return res; }, {});<p>The result is an object with the medalists as keys, and the count as values. JS objects are unordered so sorting is left as an excercise for the reader.
评论 #12296407 未加载
评论 #12293414 未加载
jpatokalalmost 9 years ago
This is you-can&#x27;t-parse-HTML-with-regex [1] level hideous, only worse, because Mediawiki markup is essentially a Turing-complete programming language thanks to template inclusion, parser functions [2], etc.<p>The only remotely sane way to do this is to use the Mediawiki API [3] to get the pages you want, then use an actual parser like mwlib [4] to extract the content you need. Wikidata and DBpedia are also promising efforts, but both have a long way to go in terms of coverage.<p>[1] <a href="http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;1732348&#x2F;regex-match-open-tags-except-xhtml-self-contained-tags" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;1732348&#x2F;regex-match-open-...</a><p>[2] <a href="https:&#x2F;&#x2F;www.mediawiki.org&#x2F;wiki&#x2F;Help:Extension:ParserFunctions" rel="nofollow">https:&#x2F;&#x2F;www.mediawiki.org&#x2F;wiki&#x2F;Help:Extension:ParserFunction...</a><p>[3] <a href="https:&#x2F;&#x2F;www.mediawiki.org&#x2F;wiki&#x2F;API:Main_page" rel="nofollow">https:&#x2F;&#x2F;www.mediawiki.org&#x2F;wiki&#x2F;API:Main_page</a><p>[4] <a href="https:&#x2F;&#x2F;www.mediawiki.org&#x2F;wiki&#x2F;Alternative_parsers" rel="nofollow">https:&#x2F;&#x2F;www.mediawiki.org&#x2F;wiki&#x2F;Alternative_parsers</a>
评论 #12296010 未加载
评论 #12295263 未加载
betolinkalmost 9 years ago
...or we can just use SPARQL and dbpedia!(<a href="http:&#x2F;&#x2F;wiki.dbpedia.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;wiki.dbpedia.org&#x2F;</a>) There are questions where you&#x27;ll have to scrap more than one page to get an answer and things could get really complicated with shell commands.<p>dbpedia is a triple-store that allows us to perform simple queries against wikipedia data like listing music bands based on a particular city:<p><pre><code> SELECT ?name ?place WHERE { ?place rdfs:label &quot;Denver&quot;@en . ?band dbo:hometown ?place . ?band rdf:type dbo:Band . ?band rdfs:label ?name . FILTER langMatches(lang(?name),&#x27;en&#x27;) } </code></pre> or queries that involve multiple subjects, categories etc.
评论 #12294541 未加载
评论 #12294736 未加载
minimaxiralmost 9 years ago
&gt; You will not need to open an editor and write a long script and then to have an interpreter like Node.js to run it, sometime the bash command line is just enough you need!<p>This is a bad attitude to have for working with <i>data processing</i>, where QA is necessary and the accuracy of the output is important. A 50 LOC scraper with comments and explicitly-defined inputs and output from functions is far preferable to a 8 LOC scraper that those without bash knowledge will be unable to parse.<p>And the 8 LOC bash script is not much of a time savings as this post demonstrates; you still have to check each function output manually to find which data to parse &#x2F; handle edge cases.
评论 #12292976 未加载
评论 #12293756 未加载
评论 #12292961 未加载
评论 #12292925 未加载
评论 #12294362 未加载
评论 #12293241 未加载
ianseyeralmost 9 years ago
This is the kind of query that excites me for WikiData&#x27;s development.<p><a href="http:&#x2F;&#x2F;wikidata.org" rel="nofollow">http:&#x2F;&#x2F;wikidata.org</a>
评论 #12293052 未加载
Washuualmost 9 years ago
There is the other option to use Parsoid.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;wikimedia&#x2F;parsoid" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;wikimedia&#x2F;parsoid</a><p>That is MediaWiki&#x27;s official off wiki parser that can turn wikitext into HTML or HTML back into wikitext. It would be reasonably simple to hook into its API and use it for data extraction instead.
评论 #12294656 未加载
orfixalmost 9 years ago
My 2 cents: the cut&#x2F;grep lines could be replaced by a sed&#x2F;awk one-liner such as:<p>sed -n &#x27;s&#x2F;.<i>flagIOCmedalist|\[\[\([^]|]</i>\).*&#x2F;\1&#x2F;p&#x27;
评论 #12294072 未加载
CydeWeysalmost 9 years ago
There is an active project sponsored by the Wikimedia Foundation called PyWikiBot that I&#x27;ve been a contributor to and user of for over a decade now. If you want to do anything and everything with Wikipedia, look no further than: <a href="https:&#x2F;&#x2F;github.com&#x2F;wikimedia&#x2F;pywikibot-core" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;wikimedia&#x2F;pywikibot-core</a>
mickael-kerjeanalmost 9 years ago
You should try wikidata for any type of query that can&#x27;t be answer using google and where all the information itself is already on wikipedia. it&#x27;s way faster (if you know about sparql) and way more powerfull and flexible. It only seems surprising there isn&#x27;t more people talking about it, triplestore are awesome
davidgerardalmost 9 years ago
... there&#x27;s an API making half of this superfluous. You can do pretty much any MediaWiki reading or writing through it. (All Wikipedia bots are required to use it, for instance.)<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;w&#x2F;api.php" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;w&#x2F;api.php</a><p>The article text is a raw blob of wikitext you have to process, but you don&#x27;t have to go to stupid lengths trying to parse HTML without a browser.
评论 #12296213 未加载
tpetricekalmost 9 years ago
Extracting data from Wikipedia with type providers: <a href="http:&#x2F;&#x2F;evelinag.com&#x2F;blog&#x2F;2015&#x2F;11-18-f-tackles-james-bond&#x2F;" rel="nofollow">http:&#x2F;&#x2F;evelinag.com&#x2F;blog&#x2F;2015&#x2F;11-18-f-tackles-james-bond&#x2F;</a>
turtlebitsalmost 9 years ago
An xpath like `&#x2F;&#x2F;table&#x2F;tr&#x2F;td[2]&#x2F;a[1]&#x2F;text()` seems like it would be a lot simpler.
评论 #12293936 未加载
kaspersetalmost 9 years ago
Large part of Bioinformatics data processing involves these commands. They seem little cryptic but gets job done. I would also like to mention Datamash: <a href="https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;datamash&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;datamash&#x2F;</a>
hbogertalmost 9 years ago
Isn&#x27;t this a poster child example for the semantic web?
ShakataGaNaialmost 9 years ago
errrrrrrrrk. Extracting raw wiki-markup and trying to use it? Not the greatest of idea. The only true parser of that language is mediawiki. Doing it yourself is a recipe for a massive headache.
评论 #12295126 未加载
yarrelalmost 9 years ago
A couple of years ago I found Perl was fastest at processing Wikipedia dumps.<p>It also didn&#x27;t require having a JVM preloaded to make startup times acceptable during development (naming no other tools).<p>I do use shell tools to process data, a lot. They&#x27;re particularly good for exploratory programming and initial analysis of new datasets.
评论 #12295383 未加载
dangravellalmost 9 years ago
Or, for a lot of the structured elements, you could use DBPedia.
vram22almost 9 years ago
There is also a wikipedia library for Python. An example of its use:<p>Using the wikipedia Python library (to search for oranges :)<p><a href="https:&#x2F;&#x2F;jugad2.blogspot.in&#x2F;2015&#x2F;11&#x2F;using-wikipedia-python-library.html" rel="nofollow">https:&#x2F;&#x2F;jugad2.blogspot.in&#x2F;2015&#x2F;11&#x2F;using-wikipedia-python-li...</a><p>And there maybe libraries for other languages too, since the above library wraps a Wikipedia API:<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Wikipedia:API" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Wikipedia:API</a>
loigealmost 9 years ago
I actually added some of your alternative solutions to the bottom of the article, thanks for commenting :)
lovelearningalmost 9 years ago
Python has excellent packages like mwparserfromhell and wikitables for this kind of processing.
opensourcedudealmost 9 years ago
I appreciate this for the novelty factor, but, somebody show this dude how to use a spreadsheet!
评论 #12293069 未加载