【问题标题】:How to get First 100 characters from wikipedia api如何从 wikipedia api 获取前 100 个字符
【发布时间】:2018-11-19 05:21:09
【问题描述】:
我想从 Wikipedia API 查询中检索前 100 个文本字符。
我在 Google 和 Stack Overflow 上进行了很多搜索,但没有得到任何答案。
通过搜索,我得到了所有的文本内容,但我只需要前 100 个字符。
这是我的代码的工作 sn-p:
<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>
【问题讨论】:
标签:
javascript
jquery
mediawiki
wikipedia
wikipedia-api
【解决方案1】:
您是否尝试过使用子字符串/切片?
<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>
【解决方案2】:
您的问题与维基百科无关,但您可以使用substring() 获取第一个n 字符,即
"one two three four".substring(0, 8)
-> "one two "
在你的情况下是这样的:
i.substring(0, 100)
【解决方案3】:
由于我们只需要 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>