如何找到维基百科页面的所有部分并全部显示出来?
How to find all sections of a wikipedia page and display them all?
维基百科页面按部分划分,因此如果我使用 API 并说 section=0
,我将阅读该部分中的文本。但是一个页面可能有很多部分,我如何循环所有的部分来加载它们?
Here I get section=0 only
HTML
<button id="wiki" data-subject="Paris">Wikipedia</button>
<output id="results">
<ul>
</ul>
</output>
JS
$("ul").on("click", "a", function(e) {
e.preventDefault();
e.stopPropagation();
});
$("#wiki").one('click', function(e) {
var articleName = $(this).data('subject');
$.getJSON("https://en.wikipedia.org/w/api.php?callback=?", {
srsearch: articleName,
action: "query",
list: "search",
format: "json"
}, function(data) {
$("#results ul").empty();
$("#results ul").append("<h3>Results for <b>" + articleName + "</b></h3>").text();
$.each(data.query.search, function(i, item) {
$("#results").append("<div><a href='https://en.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "' data-toggle='modal' data-target='.bs-example-modal-lg'>" + item.title + "</a><br>" + item.snippet + "</div");
var myLink = $("#results a").attr("href");
$("#results div a").attr("href", "#");
});
$('.modal').on('show.bs.modal', function (e) {
$.getJSON("https://en.wikipedia.org/w/api.php?action=parse§ion=0&prop=text&format=json&callback=?", {
page: e.relatedTarget.textContent
}, function(data) {
var markup = data.parse.text["*"];
var blurb = $('<div></div>').html(markup);
blurb.find('a').each(function() {
$(this).replaceWith($(this).html());
});
blurb.find('sup').remove();
blurb.find('.mw-ext-cite-error').remove();
$(".modal-header .modal-title").html(articleName);
$(".modal-header .modal-title").promise().done(function(){
$(".modal-title").css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});
});
$(".modal-body").html($(blurb).find("p"));
$(".modal-body").promise().done(function(){
$(".modal-body").css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});
});
});
});
});
});
只需删除 section=0
即可:
$.getJSON("https://it.wikipedia.org/w/api.php?format=json&action=parse&prop=text&callback=?", {...
维基百科页面按部分划分,因此如果我使用 API 并说 section=0
,我将阅读该部分中的文本。但是一个页面可能有很多部分,我如何循环所有的部分来加载它们?
Here I get
section=0 only
HTML
<button id="wiki" data-subject="Paris">Wikipedia</button>
<output id="results">
<ul>
</ul>
</output>
JS
$("ul").on("click", "a", function(e) {
e.preventDefault();
e.stopPropagation();
});
$("#wiki").one('click', function(e) {
var articleName = $(this).data('subject');
$.getJSON("https://en.wikipedia.org/w/api.php?callback=?", {
srsearch: articleName,
action: "query",
list: "search",
format: "json"
}, function(data) {
$("#results ul").empty();
$("#results ul").append("<h3>Results for <b>" + articleName + "</b></h3>").text();
$.each(data.query.search, function(i, item) {
$("#results").append("<div><a href='https://en.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "' data-toggle='modal' data-target='.bs-example-modal-lg'>" + item.title + "</a><br>" + item.snippet + "</div");
var myLink = $("#results a").attr("href");
$("#results div a").attr("href", "#");
});
$('.modal').on('show.bs.modal', function (e) {
$.getJSON("https://en.wikipedia.org/w/api.php?action=parse§ion=0&prop=text&format=json&callback=?", {
page: e.relatedTarget.textContent
}, function(data) {
var markup = data.parse.text["*"];
var blurb = $('<div></div>').html(markup);
blurb.find('a').each(function() {
$(this).replaceWith($(this).html());
});
blurb.find('sup').remove();
blurb.find('.mw-ext-cite-error').remove();
$(".modal-header .modal-title").html(articleName);
$(".modal-header .modal-title").promise().done(function(){
$(".modal-title").css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});
});
$(".modal-body").html($(blurb).find("p"));
$(".modal-body").promise().done(function(){
$(".modal-body").css({opacity: 0.0, visibility: "visible"}).animate({opacity: 1.0});
});
});
});
});
});
只需删除 section=0
即可:
$.getJSON("https://it.wikipedia.org/w/api.php?format=json&action=parse&prop=text&callback=?", {...