1

I've searched far and wide for this and unfortunately haven't been able to find an answer. I'm trying to fetch table elements from Wikipedia pages using the Wikipedia API and JavaScript/jQuery. For example, take the page below:

https://en.wikipedia.org/wiki/2017_NFL_Draft

If I want to be able to use the API to fetch just one cell of the table (eg round 1, pick #7, or all players drafted from UCLA) is there a way to do this? Because each page of these is the same, think this will be easy to do one call across pages (just need to increment the url by 1), but I haven't had any luck using the API to fetch anything except for a section.

I tried the test call which shows me the table:

https://en.wikipedia.org/w/api.php?action=parse&page=2017_NFL_Draft&section=2&prop=text

And I see the specific elements I'd like to grab but can't figure out how. Is there anyway to do this? Would I be better off just copying/pasting the data in excel and putting it in a form I want?

Thanks for all of your help!

DietDrB
  • 13
  • 2
  • See https://stackoverflow.com/a/15403888/1110636 – Timir May 06 '18 at 19:40
  • Possible duplicate of [jQuery() not finding elements in jQuery.parseHTML() result](https://stackoverflow.com/questions/15403600/jquery-not-finding-elements-in-jquery-parsehtml-result) – Timir May 06 '18 at 19:41

1 Answers1

1

I believe this is what you are looking for. I am pulling in the main table . You can refine your element queries inside my $table object

var params = {
  origin: '*',
  action: 'parse',
  page: '2017_NFL_Draft',
  section: 2,
  prop: 'text',
  format: 'json'
};

$.getJSON('https://en.wikipedia.org/w/api.php', params).then(res => {
  var $table = $(res.parse.text['*']).find('table').eq(3)
  $('body').append($table)
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Wow this is great! Thanks so much...just what I need to get me started. One question...what does the "origin" here denote with the " * "? Unclear from the wiki documentation. – DietDrB May 06 '18 at 22:18
  • Oh that is a requirement for CORS from https://www.mediawiki.org/wiki/Manual:CORS. Is a pain to track down. I also had to do a search on this site and found that using `*` as origin works – charlietfl May 06 '18 at 22:22
  • Suggest you start a new question for that and in that question provided the source html – charlietfl May 27 '18 at 21:15
  • Thanks but think I figured it out actually, or rather, am close to figuring it out (so I deleted the question)! Appreciate your help here, absolutely huge. – DietDrB May 27 '18 at 22:30