简体   繁体   English

对Wikipedia API搜索FreeCodecamp感到困惑

[英]Confused about Wikipedia api search freecodecamp

I'm trying to write the code in which one enter a word, click the button (I'll add the code later for submitting by pressing "return" button), and you get 10 results related to the word you enter. 我正在尝试编写其中输入一个单词的代码,单击按钮(稍后我将添加代码以通过按“返回”按钮提交),您将获得10个与您输入的单词相关的结果。 Each result will have a title/url and a description (basically, the first sentence in the page, I think?). 每个结果都会有一个标题/ URL和一个描述(我想基本上是页面中的第一句话?)。 I tried to use API Sandbox to figure out what I need but the results I got wasn't what I was looking for. 我试图使用API Sandbox找出我需要的东西,但是得到的结果却不是我想要的。 I also still don't get the difference between list=search and generator=search since they both return the results. 我仍然没有得到list=searchgenerator=search之间的区别,因为它们都返回结果。 However, it seems that I got more success when I use list=search when I call the title in json as data.search[i].title and description(?) as data.search[i].snippet . 但是,当我将json中的标题称为data.search [i] .title和description(?)称为data.search [i] .snippet时,当我使用list=search时,似乎获得了更大的成功。 However, I'm unable to call the info when I use generator=search . 但是,当我使用generator=search时,我无法调用该信息。 The documentation for MediaWiki API was way too confusing... Basically, I wanted to call the extract of the info which seems to only happen if I use generator=search . MediaWiki API的文档太让人困惑了……基本上,我想调用信息的摘录,这似乎只有在使用generator=search情况下才会发生。 However, I got an error when I tried to call the title as data.pages[i].title and description as data.pages[i].extract . 但是,当我尝试将标题称为data.pages[i].title和description称为data.pages[i].extract

Here's my code: 这是我的代码:

 $(document).ready(function() { function getWord() { var keyword = $("#searchkeyword").val(); $.ajax({ url: "https://en.wikipedia.org/w/api.php?", type: 'GET', dataType: "jsonp", data: { action: 'query', format: 'json', prop: 'extracts', list: 'search', srsearch: keyword, exsentences: '1', exlimit: '10', exintro: '1', explaintext: '1' }, success: function(data) { console.log(data); $("#articlearea").empty(); for(var i = 0; i < 10; i++) { $("#articlearea").append('<div class="articlebox">' + data.query.search[i].title + '<br>' + data.query.search[i].extract + '</div>'); } } }); }; $("#submitbtn").on("click", getWord); }); 
 @import url('https://fonts.googleapis.com/css?family=Amiko:400,600'); body { position: relative; font-family: 'Amiko', sans-serif; } html, body{ height:100%; margin: 0; background-color: #40e0d0; } .wrapper { text-align: center; position: relative; } .container { width: 75%; margin: 30px auto 0; } .container a { color: #ffffff; font-size: 1.1em; text-decoration: none; margin-bottom: 10px; display: block; } input { border: 1px solid #ffffff; border-radius: 4px; padding: 5px 8px; font-size: 1.3em; } #submitbtn { position: relative; z-index: 1; left: -32px; top: -2px; color: #7B7B7B; cursor:pointer; width: 0; } .fa-search { font-size: 1.3em; } 
 <div class="wrapper"> <div class="container"> <a id="randomlink" href="https://en.wikipedia.org/wiki/Special:Random" target="_blank">Click here for a random Wikipedia article</a> <form> <input id="searchkeyword" type="text" placeholder="Search Wikipedia" /> <i id="submitbtn" class="fa fa-search"></i> </form> <div id="articlearea"></div> </div> </div> 

generator=search won't work because that isn't an option you have the options generator=allpages , generator=links . generator=search无效,因为这不是选项,您可以使用以下选项: generator=allpagesgenerator=links A generator gives you either all pages of a search term or all the links. 生成器为您提供搜索词的所有页面或所有链接。 The search term is actually used with gapfrom= . search词实际上与gapfrom= Heres a working example: 这是一个工作示例:

{
    "action": "query",
    "format": "json",
    "prop": "links|categories",
    "generator": "allpages",
    "gapfrom": "Ba",
    "gaplimit": "3"
}

 $(document).ready(function() { function getWord() { var keyword = $("#searchkeyword").val(); $.ajax({ url: "https://en.wikipedia.org/w/api.php?", type: 'GET', dataType: "jsonp", data: { action: 'query', format: 'json', prop: 'info', generator: 'allpages', inprop: 'url', gapfrom: keyword, gaplimit: "3" }, success: function(data) { var pages = Object.keys(data.query.pages); $("#articlearea").empty(); pages.forEach(page => { $("#articlearea").append('<div class="articlebox">' + JSON.stringify(data.query.pages[page].title) + '<br>' + JSON.stringify(data.query.pages[page].canonicalurl) + '</div>'); }) } }); }; $("#submitbtn").on("click", getWord); }); 
 @import url('https://fonts.googleapis.com/css?family=Amiko:400,600'); body { position: relative; font-family: 'Amiko', sans-serif; } html, body { height: 100%; margin: 0; background-color: #40e0d0; } .wrapper { text-align: center; position: relative; } .container { width: 75%; margin: 30px auto 0; } .container a { color: #ffffff; font-size: 1.1em; text-decoration: none; margin-bottom: 10px; display: block; } input { border: 1px solid #ffffff; border-radius: 4px; padding: 5px 8px; font-size: 1.3em; } #submitbtn { position: relative; z-index: 1; left: -32px; top: -2px; color: #7B7B7B; cursor: pointer; width: 0; } .fa-search { font-size: 1.3em; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wrapper"> <div class="container"> <a id="randomlink" href="https://en.wikipedia.org/wiki/Special:Random" target="_blank">Click here for a random Wikipedia article</a> <form> <input id="searchkeyword" type="text" placeholder="Search Wikipedia" /> <button id="submitbtn" class="fa fa-search"></button> </form> <div id="articlearea"></div> </div> </div> 

I changed the form to add a "on submit" to automatically run your getWords() function. 我更改了表单以添加“提交时”以自动运行您的getWords()函数。 I also changed the action to javascript:void() to avoid a refresh. 我也将动作更改为javascript:void()以避免刷新。 I also changed your getWord() and took it out of the document.ready function because it's not accessible form the form "onsubmit" as it was. 我还更改了您的getWord()并将其从document.ready函数中删除,因为它无法通过“ onsubmit”形式进行访问。 Now, the code should be functioning. 现在,代码应该可以正常工作了。 If you need any more help, just comment. 如果您需要更多帮助,请发表评论。

<div class="wrapper">
<div class="container">
<a id="randomlink" href="https://en.wikipedia.org/wiki/Special:Random" target="_blank">Click here for a random Wikipedia article</a>
<form action="javascript:void(0)" onsubmit="getWord()">
  <input id="searchkeyword" type="text" placeholder="Search Wikipedia" />
  <i id="submitbtn" class="fa fa-search"></i>
</form>
<div id="articlearea"></div>
</div>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

<script type="text/javascript">

function getWord() {

var keyword = $("#searchkeyword").val();

$.ajax({
  url: "https://en.wikipedia.org/w/api.php?",
  type: 'GET',
  dataType: "jsonp",
  data: {
    action: 'query',
    format: 'json',
    prop: 'extracts',
    list: 'search',
    srsearch: keyword,
    exsentences: '1',
    exlimit: '10',
    exintro: '1',
      explaintext: '1'
  },
  success: function(data) {

  console.log(data);
   $("#articlearea").empty();

  for(var i = 0; i < 10; i++) {
      $("#articlearea").append('<div class="articlebox">' + data.query.search[i].title +  '</div>');
  }


  }
});
};

$("#submitbtn").on("click", getWord);

</script>

Actually, what I was asking was to access the data from generator=search . 实际上,我要问的是从generator=search访问数据。 I forgot that I need to go inside two times for that compared to one time for list=search . 我忘了,我需要两次进去,而list=search

All I need to do is to add this code to my javascript area: 我需要做的就是将此代码添加到我的javascript区域:

var pages = data.query.pages;

        for(var page in pages) {
          $("#articlearea").append('<div class="articlebox"><a target="_blank" href="' + pages[page].fullurl + '"><div class="articlelink"><h2>' + pages[page].title + '</h2>' + pages[page].extract + '</div></a></div>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM