栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何通过API获得简短的文本片段和Wikipedia文章的主图像?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何通过API获得简短的文本片段和Wikipedia文章的主图像?

您可以在一个请求中检索文本和图像,请尝试以下操作:

$(document).ready(function () {    var articles = $('.articles');    var input = $('input');    var button = $('button');    var toSearch = '';    var searchUrl = 'https://en.wikipedia.org/w/api.php';    var ajaxArticleData = function () {        $.ajax({ url: searchUrl, dataType: 'jsonp', data: {     //main parameters     action: 'query',     format: 'json',     generator: 'search',         //parameters for generator         gsrsearch: toSearch,         gsrnamespace: 0,         gsrlimit: 10,     prop: 'extracts|pageimages',         //parameters for extracts         exchars: 200,         exlimit: 'max',         explaintext: true,         exintro: true,         //parameters for pageimages         piprop: 'thumbnail',         pilimit: 'max',         pithumbsize: 200 }, success: function (json) {     var pages = json.query.pages;     $.map(pages, function (page) {         var pageElement = $('<div>');         //get the article title         pageElement.append($('<h2>').append($('<a>').attr('href', 'http://en.wikipedia.org/wiki/' + page.title).text(page.title)));         //get the article image (if exists)         if (page.thumbnail) pageElement.append($('<img>').attr('width', 150).attr('src', page.thumbnail.source));         //get the article text         pageElement.append($('<p>').text(page.extract));         pageElement.append($('<hr>'));         articles.append(pageElement);     }); }        });    };    input.autocomplete({        source: function (request, response) { $.ajax({     url: searchUrl,     dataType: 'jsonp',     data: {         'action': "opensearch",         'format': "json",         'search': request.term     },     success: function (data) {         response(data[1]);     } });        }    });    button.click(function () {        articles.empty();        toSearch = input.val();        ajaxArticleData();    });});<!DOCTYPE html><html><head>    <title>My Wikipedia Viewer</title>    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" /></head><body>    <section>        <input type='text' value='' placeholder='Search for...'>        <button>Search</button>    </section>    <section ></section>    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.min.js"></script></body></html>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/418269.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号