0

I am making a website that will automatically get the data from wikipedia using Wikipedia API. What I have already tries is to use find('.infobox') to find the infobox data.

However, I only want to get the data below the horizontal line in the infobox(starting from disease: covid in the photo below)

Here is the code I currently had:

let area = "Canada"
var url="http://en.wikipedia.org/w/api.php?action=parse&format=json&page=COVID-19_pandemic_in_"+area +"&redirects&prop=text&callback=?";
$.getJSON(url,function(data){
     wikiHTML = data.parse.text["*"];
$wikiDOM = $("<document>"+wikiHTML+"</document>");
var info= $wikiDOM.find('.infobox').html();
console.log(info)

This will display all the information in the infobox.

Could anyone give me an idea or a solution that tell me how to do that?

Also, the image from wikipedia will not be able to display which will show error of invalid url (the image area will display alt text) Is there a way to choose not to display image before displaying which will prevent the image (alt text) and error in console ?

Thanks for any responds!

*In the image below, I only want to get the data from disease: Covid-19 to Vaccinations

enter image description here

Yusuf
  • 79
  • 6
  • @Tgr, I have checked this answer before I asked the question. But fortunately, the answer below has solved my problem – Yusuf Dec 07 '21 at 23:42

1 Answers1

2

The solution is to filter element depending on the conditions.

I only want to get the data from disease: Covid-19 to Vaccinations

You can look at the element that encloses these information and find things in common. I see each element includes th.infobox-label.

Check if other element does not have the same structure.

If not, filtering the element by whether the element includes th.infobox-label will work out.

Kouta Nakano
  • 595
  • 3
  • 15