如何获得维基百科的第一段解析内容? [英] How to get the first parsed paragraph of wikipedia?

查看:116
本文介绍了如何获得维基百科的第一段解析内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我正在使用的 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&section=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

我根本没有内容进入文本区域

I am getting no content into the textarea at all

推荐答案

我对代码进行了一些调整,使之能够正常工作(您可以在下面看到新的小提琴.

There were a few adjustments I made to the code and I got it to work (you can see the new fiddle below.

  1. 我在URL中添加了page参数.到目前为止还没有添加,因此您最初的小提琴是从Wikipedia收到错误响应.这意味着一开始就没有文章文字可以使用.我还对其进行了uri编码.
  2. 我更改了指定JsonP回调的方式. jQuery正确地将callback参数添加到URL,而Wikipedia结果正确地返回了对其的调用,但不会被执行.使用callback=?并让jQuery通过使用success选项来处理回调.
  3. 我将Roman Empire切换为Impero romano,这是您在Wikipedia的IT版本中可以找到的文章.
  4. 我将val切换为text.在textareas的情况下,它们不将value作为其他输入,而是文本内容.我知道,不一致.
  1. I added the page parameter to the URL. It wasn't added so far so your initial fiddle was getting an error response from Wikipedia. This meant that there was no article text to work with in the first place. I also uri-encoded it.
  2. I changed the way that the JsonP callback was being specified. jQuery was correctly adding the callback parameter to the URL and the Wikipedia result was correctly returning a call to it, but it wouldn't get executed. Using callback=? and letting jQuery handle the callback by using the success option did the trick.
  3. I switched Roman Empire for Impero romano, which is the article that you'll find in the IT version of Wikipedia.
  4. I switched val for text. In the case of textareas, they don't hold value as other inputs, but rather text content. I know, inconsistent.

$("#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&section=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>

这篇关于如何获得维基百科的第一段解析内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆