我想从Wikipedia API查询中检索前100个文本字符。
我在谷歌和Stack Overflow上搜索了很多,但都没有找到答案。通过搜索,我得到了所有的文本内容,但我只需要前100个字符。
以下是我的代码的工作片段:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="article"></div>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=0&page=Jimi_Hendrix&callback=?",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (data, textStatus, jqXHR) {
var markup = data.parse.text["*"];
var i = $('<div></div>').html(markup);
// remove links as they will not work
i.find('a').each(function() { $(this).replaceWith($(this).html()); });
// remove any references
i.find('sup').remove();
// remove cite error
i.find('.mw-ext-cite-error').remove();
$('#article').html($(i).find('p'));
},
error: function (errorMessage) {
}
});
});
</script>
您尝试过使用子字符串/切片吗?
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="article"></div>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=0&page=Jimi_Hendrix&callback=?",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (data, textStatus, jqXHR) {
var markup = data.parse.text["*"];
var i = $('<div></div>').html(markup);
// remove links as they will not work
i.find('a').each(function() { $(this).replaceWith($(this).html()); });
// remove any references
i.find('sup').remove();
// remove cite error
i.find('.mw-ext-cite-error').remove();
$('#article').html($(i).find('p').text().slice(0, 100));
},
error: function (errorMessage) {
}
});
});
</script>
您的问题与维基百科无关,但您可以使用substring()
获取第一个n
字符,即
"one two three four".substring(0, 8)
-> "one two "
在你的情况下,它会像:
i.substring(0, 100)
由于我们只需要wiki页面中文本内容中的100个字符,因此我们可以迭代段落,直到获得至少100个字符为止,然后使用方法slice
检索前100个字符。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="article"></div>
<script type="text/javascript">
$(document).ready(function(){
// extracting 100 length text content from stackoverflow page
$.ajax({
type: "GET",
url: "http://en.wikipedia.org/w/api.php?action=parse&format=json&prop=text§ion=0&page=Stack_Overflow&callback=?",
contentType: "application/json; charset=utf-8",
async: false,
dataType: "json",
success: function (data, textStatus, jqXHR) {
var markup = data.parse.text["*"];
var i = $('<div></div>').html(markup);
// remove links as they will not work
i.find('a').each(function() { $(this).replaceWith($(this).html()); });
// remove any references
i.find('sup').remove();
// remove cite error
i.find('.mw-ext-cite-error').remove();
// whole paragraphs
var paragraphs = $(i).find('p');
// convert whole paragraphs to string
var str = "";
for (var i = 0; i < paragraphs.length; ++i) {
str += paragraphs[i].textContent;
// break as soon as we get required length
if (str.length >= 100 ) break;
}
$('#article').html(str.slice(0,100));
},
error: function (errorMessage) {
}
});
});
</script>