I want to get the first paragraph of a Wikipedia article.
What is the API query to do so?
I want to get the first paragraph of a Wikipedia article.
What is the API query to do so?
See this section on the MediaWiki docs
These are the key parameters.
prop=revisions&rvprop=content&rvsection=0
rvsection = 0 specifies to only return the lead section.
See this example.
http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&rvsection=0&titles=pizza
To get the HTML, you can use similarly use action=parse http://en.wikipedia.org/w/api.php?action=parse§ion=0&prop=text&page=pizza
Note, that you'll have to strip out any templates or infoboxes.
See Is there a clean wikipedia API just for retrieve content summary? for other proposed solutions. Here is one that I suggested:
There is actually a very nice prop called extracts that can be used with queries designed specifically for this purpose. Extracts allow you to get article extracts (truncated article text). There is a parameter called exintro that can be used to retrieve the text in the zeroth section (no additional assets like images or infoboxes). You can also retrieve extracts with finer granularity such as by a certain number of characters (exchars) or by a certain number of sentences(exsentences)
Here is a sample query http://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=Stack%20Overflow and the API sandbox http://en.wikipedia.org/wiki/Special:ApiSandbox#action=query&prop=extracts&format=json&exintro=&titles=Stack%20Overflow to experiment more with this query.
Please note that if you want the first paragraph specifically you still need to get the first tag. However in this API call there are no additional assets like images to parse. If you are satisfied with this intro summary you can retrieve the text by running a function like php's strip_tag that remove the html tags.
I do it this way:
https://en.wikipedia.org/w/api.php?action=opensearch&search=bee&limit=1&format=json
The response you get is an array with the data, easy to parse:
[
"bee",
[
"Bee"
],
[
"Bees are flying insects closely related to wasps and ants, known for their role in pollination and, in the case of the best-known bee species, the European honey bee, for producing honey and beeswax."
],
[
"https://en.wikipedia.org/wiki/Bee"
]
]
To get just the first paragraph limit=1
is what you need.
If you need to do this for a large number of articles, then instead of querying the website directly, consider downloading a Wikipedia database dump and then accessing it through an API such as JWPL.
You can get the introduction of the article in Wikipedia by querying pages such as https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=java. You just need to parse the json file and the result is plain text which has been cleaned including removing links and references.
<script>
function dowiki(place) {
var URL = 'https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=';
URL += "&titles=" + place;
URL += "&rvprop=content";
URL += "&callback=?";
$.getJSON(URL, function (data) {
var obj = data.query.pages;
var ob = Object.keys(obj)[0];
console.log(obj[ob]["extract"]);
try{
document.getElementById('Label11').textContent = obj[ob]["extract"];
}
catch (err) {
document.getElementById('Label11').textContent = err.message;
}
});
}
</script>
You can download the Wikipedia database directly and parse all pages to XML with Wiki Parser, which is a standalone application. The first paragraph is a separate node in the resulting XML.
Alternatively, you can extract the first paragraph from its plain-text output.
You can use JQuery to do that. First create the url with appropriate parameters. Check this link to understand what the parameters mean. Then use $.ajax()
method to retrieve the articles. Note that wikipedia does not allow cross origin request. That's why we are using dataType : jsonp
in the request.
var wikiURL = "https://en.wikipedia.org/w/api.php";
wikiURL += '?' + $.param({
'action' : 'opensearch',
'search' : 'your_search_term',
'prop' : 'revisions',
'rvprop' : 'content',
'format' : 'json',
'limit' : 10
});
$.ajax( {
url: wikiURL,
dataType: 'jsonp',
success: function(data) {
console.log(data);
}
} );
You can use the extract_html
field of the summary REST endpoint for this: e.g. https://en.wikipedia.org/api/rest_v1/page/summary/Cat.
Note: This aims to simply the content a bit by removing most of the pronunciations, mainly in parentheses in some cases.