如何获取维基百科的第一个解析段落?
How to get the first parsed paragraph of wikipedia?
这是我正在使用的js:
function onSuccess(data){
var markupt = data.parse.text["*"];
$('#usp-custom-4').val(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();
}
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
<div id="headingWiki_0"><h3><span>Roman empire</span></h3></div>
<button id="wiki">Load</button>
<textarea id="usp-custom-4"></textarea>
这是一个jsFiddle
我在文本区域中根本没有内容
我对代码做了一些调整,我让它工作了(你可以在下面看到新的 fiddle。
- 我将
page
参数添加到 URL。到目前为止还没有添加,所以您最初的 fiddle 收到来自维基百科的错误响应。这意味着一开始就没有文章文本可供使用。我也对它进行了uri编码。
- 我更改了指定 JsonP 回调的方式。 jQuery 正确地将
callback
参数添加到 URL 并且维基百科结果正确地返回了对它的调用,但它不会被执行。使用 callback=?
并让 jQuery 通过使用 success
选项处理回调就成功了。
- 我将
Roman Empire
换成 Impero romano
,这是您可以在 IT 版维基百科中找到的文章。
- 我把
val
换成了 text
。在文本区域的情况下,它们不保存 value
作为其他输入,而是保存文本内容。我知道,不一致。
$("#wiki").on("click", function(){
firstWiki();
});
function onSuccess(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();
}
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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="headingWiki_0"><h3><span>Impero romano</span></h3></div>
<button id="wiki">
Load
</button>
<textarea id="usp-custom-4"></textarea>
这是我正在使用的js:
function onSuccess(data){
var markupt = data.parse.text["*"];
$('#usp-custom-4').val(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();
}
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
<div id="headingWiki_0"><h3><span>Roman empire</span></h3></div>
<button id="wiki">Load</button>
<textarea id="usp-custom-4"></textarea>
这是一个jsFiddle
我在文本区域中根本没有内容
我对代码做了一些调整,我让它工作了(你可以在下面看到新的 fiddle。
- 我将
page
参数添加到 URL。到目前为止还没有添加,所以您最初的 fiddle 收到来自维基百科的错误响应。这意味着一开始就没有文章文本可供使用。我也对它进行了uri编码。 - 我更改了指定 JsonP 回调的方式。 jQuery 正确地将
callback
参数添加到 URL 并且维基百科结果正确地返回了对它的调用,但它不会被执行。使用callback=?
并让 jQuery 通过使用success
选项处理回调就成功了。 - 我将
Roman Empire
换成Impero romano
,这是您可以在 IT 版维基百科中找到的文章。 - 我把
val
换成了text
。在文本区域的情况下,它们不保存value
作为其他输入,而是保存文本内容。我知道,不一致。
$("#wiki").on("click", function(){
firstWiki();
});
function onSuccess(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();
}
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;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="headingWiki_0"><h3><span>Impero romano</span></h3></div>
<button id="wiki">
Load
</button>
<textarea id="usp-custom-4"></textarea>