How To Get The First Parsed Paragraph Of Wikipedia?
This is the js I am using: function onSuccess(data){ var markupt = data.parse.text['*']; $('#usp-custom-4').val(markupt); console.log(markupt); var blurbt =
Solution 1:
There were a few adjustments I made to the code and I got it to work (you can see the new fiddle below.
- I added the
page
parameter to the URL. It wasn't added so far so your initial fiddle was getting an error response from Wikipedia. This meant that there was no article text to work with in the first place. I also uri-encoded it. - I changed the way that the JsonP callback was being specified. jQuery was correctly adding the
callback
parameter to the URL and the Wikipedia result was correctly returning a call to it, but it wouldn't get executed. Usingcallback=?
and letting jQuery handle the callback by using thesuccess
option did the trick. - I switched
Roman Empire
forImpero romano
, which is the article that you'll find in the IT version of Wikipedia. - I switched
val
fortext
. In the case of textareas, they don't holdvalue
as other inputs, but rather text content. I know, inconsistent.
$("#wiki").on("click", function(){
firstWiki();
});
functiononSuccess(data){
var markupt = data.parse.text["*"];
$('#usp-custom-4').text(markupt);
console.log(markupt);
var blurbt = $('<div></div>').html(markupt);
blurbt.find(".mw-editsection, #toc, .noprint, .thumb, img, table").remove();
// remove links as they will not work
blurbt.find('a').each(function() { $(this).replaceWith($(this).html()); });
// remove any references
blurbt.find('sup').remove();
// remove cite error
blurbt.find('.mw-ext-cite-error').remove();
var pOnly = $(blurbt).find('p').text();
}
functionfirstWiki() {
var titolo = $("#headingWiki_0 h3 span").text();
titolo = encodeURIComponent(titolo);
$.ajax({
url: "https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=0&page=" + titolo + "&callback=?",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: onSuccess
});
}
textarea {
width: 100%;
height: 200px;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox] + label {
background: #999;
display: inline-block;
padding: 0;
}
input[type=checkbox]:checked + label {
border: 10px solid grey;
padding: 0;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divid="headingWiki_0"><h3><span>Impero romano</span></h3></div><buttonid="wiki">
Load
</button><textareaid="usp-custom-4"></textarea>
Post a Comment for "How To Get The First Parsed Paragraph Of Wikipedia?"