7

I'm trying to import this table from a wikipedia page containing the list of lunar missions into a Mathematica as an Association. Here, I'm trying to use the new WikipediaData function, but it doesn't seem to work.

WikipediaData["PageID" -> "656253283", "ArticleWikicode"]
Mahdi
  • 1,619
  • 10
  • 23
M.R.
  • 31,425
  • 8
  • 90
  • 281
  • When I use your code, it doesn't work but WikipediaData["List of missions to the Moon", "ArticleWikicode"] works fine. – Mahdi May 06 '15 at 17:15
  • Oh PageID is wrong, the correct one is 38293476. – Mahdi May 06 '15 at 17:17
  • 1
    You can alwars just use Import : Import["http://en.wikipedia.org/wiki/List_of_missions_to_the_Moon", {"HTML", "Source"}] . ( WikipediaData doesn't work for me either by the way ) – george2079 May 06 '15 at 17:45
  • 1
    This package could be used. The third example in that post is actually a table on Wikipedia. – C. E. May 07 '15 at 00:22
  • 1
    The function you are using is not going to work because that's not the PageID of the article you are trying to import. The correct one is 38293476, so, WikipediaData["PageID" -> "38293476", "ArticleWikicode"] works fine. But as Mahdi said, you can also use the actual title as in WikipediaData["List of missions to the Moon", "ArticleWikicode"] – xtian777x Aug 27 '15 at 03:57

2 Answers2

6

my stab at it..

 source = Import[
    "http://en.wikipedia.org/wiki/List_of_missions_to_the_Moon",
            {"HTML", "Source"}];
 tstart = StringPosition[source, 
        "<table class=\"wikitable sortable\"" ][[1, 1]];
 tend = Select[ 
          StringPosition[source, "</table>"] , #[[1]] > tstart & ][[1, 2]];
 tabledata = 
       ImportString[
          StringTake[ source , {tstart, tend} ], {"html", "Data"}];
 Grid@tabledata

If there are more elegant ways of parsing html I'd like to see..

By the way WikipediaData gives me these sorts of errors no matter what I try:

" ServiceObject::noget: The parameter Wikicode is not available for the service Wikipedia. >> "

george2079
  • 38,913
  • 1
  • 43
  • 110
5

As george2079 said, you can use Import:

text = Import["https://en.wikipedia.org/wiki/List_of_missions_to_the_Moon", "Data"];

and then naively because table has 7 columns:

table = Cases[text, {_, _, _,_, _, _, _}, ∞];

But you need to do some clean-up afterwards!

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
Mahdi
  • 1,619
  • 10
  • 23