Wikipedia API for a single category search

Wikipedia API for a single category search
0

#1

I’ve been trying to implement an app that searches for wikipedia articles within a certain category using the wikipedia api. I already have an app that uses the api, but it searches all of wikipedia.

I’ve had a great deal of trouble finding the correct syntax for my project. The api returns the same result, no matter what I search. Thanks so much!

Here’s my code so far:

    var url =
  "https://en.wikipedia.org/w/api.php?action=query&search=" + "force" +
  "&list=categorymembers&cmtitle=Category:Physics&format=json&callback=wikiCallback";
var displayWikiData = function() {
  $.ajax({
    url: url,
    type: "GET",
    dataType: "jsonp",
    jsonp: "callback",
    success: function(res) {
      console.log(res);
    }
  });
}

Here’s what the console logs:

query: {…}
​​categorymembers: (10) […]
​​​0: Object { pageid: 22939, ns: 0, title: "Physics" }
​​​1: Object { pageid: 24489, ns: 0, title: "Outline of physics" }
​​​2: Object { pageid: 3445246, ns: 0, title: "Glossary of classical physics" }
​​​3: Object { pageid: 1653925, ns: 100, title: "Portal:Physics" }
​​​4: Object { pageid: 50926902, ns: 0, title: "Action angle coordinates" }
​​​5: Object { pageid: 9079863, ns: 0, title: "Aerometer" }
​​​6: Object { pageid: 52657328, ns: 0, title: "Bayesian model of computational anatomy" }
​​​7: Object { pageid: 49342572, ns: 0, title: "Group actions in computational anatomy" }
​​​8: Object { pageid: 50724262, ns: 0, title: "Blasius–Chaplygin formula" }
​9: Object { pageid: 33327002, ns: 0, title: "Cabbeling" }
​​​length: 10

#2

What you should be seeing in the console is

{
  "warnings": {
    "main": {
      "*": "Unrecognized parameter: search."
    }
  },
  "batchcomplete": "",
  "continue": {
    "cmcontinue": "page|2d2941313f2b292d3d0447454f31434f39293f011701dc16|55503653",
    "continue": "-||"
  },
  "query": {
    "categorymembers": [
      {
        "pageid": 22939,
        "ns": 0,
        "title": "Physics"
      },
      {
        "pageid": 24489,
        "ns": 0,
        "title": "Outline of physics"
      },
      {
        "pageid": 3445246,
        "ns": 0,
        "title": "Glossary of classical physics"
      },
      {
        "pageid": 1653925,
        "ns": 100,
        "title": "Portal:Physics"
      },
      {
        "pageid": 50926902,
        "ns": 0,
        "title": "Action angle coordinates"
      },
      {
        "pageid": 9079863,
        "ns": 0,
        "title": "Aerometer"
      },
      {
        "pageid": 52657328,
        "ns": 0,
        "title": "Bayesian model of computational anatomy"
      },
      {
        "pageid": 49342572,
        "ns": 0,
        "title": "Group actions in computational anatomy"
      },
      {
        "pageid": 50724262,
        "ns": 0,
        "title": "Blasius–Chaplygin formula"
      },
      {
        "pageid": 33327002,
        "ns": 0,
        "title": "Cabbeling"
      }
    ]
  }
}

In the API, continue and cmcontinue indicate that more results are available, so you would have to make additional requests until you have all of the results. You could also make a request first to see information about a category:

Physics Category Info

Go thru the API documentation more and use the API sandbox to try out different queries.

Also note the warnings indicating that the search parameter is not recognized and is not needed for this query.

Hope this helps…


#3

I should clarify my question: I would like the user to be able to search any query, but only return results that are within a certain category. For example, Groucho Marx within physics would return undefined, although a page exists on wikipedia.
Is there a way to modify the API Call to automatically return this, or would I have to get a list of pages within the category and filter through the results to see which contain the said query? Thanks a lot for your time.


#4

Would this work?

https://en.wikipedia.org/w/api.php?action=query&list=search&format=jsonfm&srsearch=force+incategory:Physics


#5

Makes sense, but the console says
Loading failed for the <script> with source “https://en.wikipedia.org/w/api.php?action=query&list=search&format=jsonfm&srsearch=force+incategory:Physics&callback=jQuery331007309460574057358_1533928628795&_=1533928628796”
And
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://en.wikipedia.org/w/api.php?action=query&list=search&format=jsonfm&srsearch=force+incategory:Physics. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).


#6

You can try prefixing the url with https://cors-anywhere.herokuapp.com/ or JSONP to request the url.