4

Let's say I have a list of Astronauts and I want to display their biographies using Wikipedia API.
So far I've tried this one:

https://en.wikipedia.org/w/api.php?action=parse&prop=wikitext&page=Nick%20Hague

Works as expected. But take a look at this example:

https://en.wikipedia.org/w/api.php?action=parse&prop=wikitext&page=Andrew%20Morgan

As you can see, there are more than one "Andrew Morgan" and that's the problem. How do I get access to the "Andrew R. Morgan" information, In case that he's the NASA astronaut.
Note that "Andrew Morgan" is just an example and It may change.These names will be sent to me from another API. so I can't change their name manually every time.

Mosrainis
  • 470
  • 6
  • 14

2 Answers2

2

Disambiguation pages are all categorized as "All disambiguation pages", so you can check for that category to see if you're on a disambiguation page.

Thus, you could check if "All_disambiguation_pages" exists as a category to determine if you're on a disambiguation page. Using the query https://en.wikipedia.org/w/api.php?action=parse&prop=categories&page=Andrew%20Morgan:

for (category of r.parse.categories) {
    if (Object.values(category).includes("All_disambiguation_pages")) {
        // we know it's a disambiguation page
    }
}

Alternatively, you could also check for the "Disambiguation" property, using the query:

https://en.wikipedia.org/w/api.php?action=query&prop=pageprops&ppprop=disambiguation&redirects&format=xml&titles=Andrew%20Morgan

Of course, these only tell you if the page is a disambiguation page. Ultimately, you need to know what you're looking for. In the case of "Andrew Morgan", the astronaut is under "Andrew R. Morgan". But some articles might use "John Doe (Astronaut)" or some other title. There's no real standardization for this.

For the example of "astronaut", you could perhaps search the disambiguation page for the keyword "astronaut", and then go to that article:

fetch('https://en.wikipedia.org/w/api.php?action=opensearch&search=andrew%20morgan&format=json&origin=*')
  .then(function(response) {
    response.json().then(function(data) {
      // data[1] is the array of titles, [2] is the array of descriptions, [3] is the array of links
      let articleUrl = data[3][data[2].findIndex(element => element.includes("astronaut"))];
      if (articleUrl !== -1) { // -1 would be not found
        console.log(articleUrl); //the url
      }
    });
  });
JoshG
  • 6,472
  • 2
  • 38
  • 61
0

You can access information of "Andrew R. Morgan" in this manner:

https://en.wikipedia.org/w/api.php?action=parse&prop=wikitext&page=Andrew%20R.%20Morgan

A space is represented by %20

Quentin
  • 1,865
  • 2
  • 24
  • 40
  • It's not possible because "Andrew Morgan" is just an example. These names will be sent to me from another API, so every time I don't know which name is right. take a look at [this](https://en.wikipedia.org/w/api.php?action=parse&prop=wikitext&page=Alexander%20Skvortsov) – Mosrainis Sep 23 '19 at 12:15