如何获得维基百科的第一个解析段?
这是我正在使用的js :
function onSuccess(data){ var markupt = data.parse.text["*"]; $('#usp-custom-4').val(markupt); console.log(markupt); var blurbt = $('').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(); } function firstWiki() { var titolo = $("#headingWiki_0 h3 span").text(); $.ajax({ url: "https://it.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=0", page: titolo, dataType: "jsonp", jsonpCallback: "onSuccess" }); }
和HTML
Roman empire
这是一个jsFiddle
我根本没有收到textarea的内容
我对代码进行了一些调整,然后我开始工作(你可以看到下面的新小提琴。
- 我将
page
参数添加到URL。 到目前为止它没有被添加,所以你的初级小提琴得到了维基百科的错误回复。 这意味着首先没有文章文本可供使用。 我也对它进行了编码。 - 我改变了指定JsonP回调的方式。 jQuery正确地将
callback
参数添加到URL并且Wikipedia结果正确地返回对它的调用,但它不会被执行。 使用callback=?
并让jQuery通过使用success
选项来处理回调。 - 我将
Roman Empire
换成了Impero romano
,这是你可以在维基百科的IT版本中找到的文章。 - 我为
text
切换了val
。 在textareas的情况下,它们不像其他输入那样具有value
,而是文本内容。 我知道,不一致。
$("#wiki").on("click", function(){ firstWiki(); }); function onSuccess(data){ var markupt = data.parse.text["*"]; $('#usp-custom-4').text(markupt); console.log(markupt); var blurbt = $('').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(); } function firstWiki() { 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; }
Impero romano