fetch wikipedia article in javascript

To fetch a Wikipedia article in JavaScript, you can use the Wikipedia API. Here's an example using the fetch() function to make an API call and retrieve the content of a Wikipedia page.

const url = 'https://en.wikipedia.org/w/api.php?action=parse&page=JavaScript&prop=text&formatversion=2&format=json';

fetch(url)
  .then(response => response.json())
  .then(data => {
    const content = data.parse.text;
    console.log(content);
  })
  .catch(error => console.error(error));
293 chars
10 lines

In this example, we're making a GET request to the Wikipedia API and passing in the page title JavaScript as a parameter. We're also specifying that we want to retrieve the text content of the page and format the response as JSON.

Once we receive a response from the API, we parse the JSON data using response.json() and then retrieve the content of the article from the text property of the parsed data.

Note that the Wikipedia API is subject to rate limiting, so you may need to implement an API key or caching strategy to avoid being blocked.

gistlibby LogSnag