gpt4 book ai didi

javascript - 搜索媒体维基

转载 作者:行者123 更新时间:2023-11-30 08:48:20 25 4
gpt4 key购买 nike

是否有任何 api 或其他方式,使用 Javascript 来搜索任何 mediawiki 并打印找到的页面(如果找不到则打印)。

我更喜欢这样的东西:

function searchWiki(wikipage, search) {
//the function here
document.write(foundPage);
}

//run it

searchWiki('https://en.wikipedia.org', 'banana');

//it would print 'https://en.wikipedia.org/wiki/Banana'

最佳答案

这是我实现的这样一个功能。它使用 MediaWiki API通过 JSONP 并且非常灵活。我想 jQuery 解决方案很好。我创建了一个 small fiddle .

searchWiki(站点、搜索、[回调]、[选项])

function searchWiki(site, search, callback, opts) {
if(typeof callback == 'object') {
opts = callback;
callback = null;
} else {
opts = opts || {};
}
// Build the required URLs
var siteUrl = (opts.ssl ? 'https' : 'http') + '://' + site;
var apiUrl = siteUrl + (opts.apiBase || '/w/') + 'api.php';
var queryUrl = apiUrl + '?action=query&list=search&srsearch=' + encodeURIComponent(search) + '&srlimit=' + (opts.maxResults || 1) + '&format=json';
// Issue the JSONP request
$.ajax(queryUrl + '&callback=?', {
dataType: 'jsonp',
// This prevents warnings about the unrecognized parameter "_"
cache: true,
success: function(data) {
// Get all returned pages
var titles = [], links = [];
for(var i = 0; i < data.query.search.length; i++) {
var title = data.query.search[i].title,
link = siteUrl + (opts.wikiBase || '/wiki/') + encodeURIComponent(title);
titles.push(title);
links.push(link);
}
if(!opts.maxResults) {
// Single result requested
if(data.query.search.length == 0) {
titles = links = null;
} else {
titles = titles[0];
links = links[0];
}
}
// Call the callback
(callback || opts.success || function(){})(titles, links);
}
});
}

示例 1:单个维基百科搜索

searchWiki('en.wikipedia.org', 'banana fruit', {
ssl: true,
success: function(title, link) {
// link is now "https://en.wikipedia.org/wiki/Banana"
if(title === null) {
$('#search-msg').text('Not found');
} else {
var anchor = $('<a>').text(title).attr('href', link);
$('#search-msg').append(anchor);
}
}
});

此示例显示了一个指向具有相关标题的维基百科页面的链接。

示例 2:多个结果

searchWiki('www.mediawiki.org', 'Release notes', {
ssl: true,
maxResults: 5,
success: function(titles, links) {
for(var i = 0; i < titles.length; i++) {
alert('MediaWiki ' + titles[i] + ' at ' + links[i]);
}
}
});

此示例最多显示五个指向与“发行说明”查询相匹配的 MediaWiki 页面的链接。

选项:

  • ssl:使用 HTTPS 而不是 HTTP
  • maxResults:返回多个(最多 n 个)结果
  • apiBase:目标站点的API目录(默认为/w/)
  • wikiBase:目标网站上的Wiki目录(默认为/wiki/)
  • success:获取结果列表后调用的函数

您可以将回调作为函数参数(在选项之前)或作为success 选项传递。


更新:这是纯 JS 解决方案(不需要 jQuery)。和 there is another fiddle ,这次没有 jQuery。

function searchWiki(site, search, callback, opts) {
if(typeof callback == 'object') {
opts = callback;
callback = null;
} else {
opts = opts || {};
}
// Build the required URLs
var siteUrl = (opts.ssl ? 'https' : 'http') + '://' + site;
var apiUrl = siteUrl + (opts.apiBase || '/w/') + 'api.php';
var queryUrl = apiUrl + '?action=query&list=search&srsearch=' + encodeURIComponent(search) + '&srlimit=' + (opts.maxResults || 1) + '&format=json';
var fnName = '_cb_' + Math.floor(Math.random() * 4294967296);
window[fnName] = function(data) {
// Clear references to this function
window[fnName] = null;
// Get all returned pages
var titles = [], links = [];
for(var i = 0; i < data.query.search.length; i++) {
var title = data.query.search[i].title,
link = siteUrl + (opts.wikiBase || '/wiki/') + encodeURIComponent(title);
titles.push(title);
links.push(link);
}
if(!opts.maxResults) {
// Single result requested
if(data.query.search.length == 0) {
titles = links = null;
} else {
titles = titles[0];
links = links[0];
}
}
// Call the callback
(callback || opts.success || function(){})(titles, links);
}
// Issue the JSONP request
var scriptTag = document.createElement('script');
scriptTag.setAttribute('src', queryUrl + '&callback=' + fnName);
document.head.appendChild(scriptTag);
}

更新 2: 最后是 node.js 的解决方案。 API 仍然相同,但它提供了一些附加选项:

  • error:错误回调(这在基于浏览器的 JS 中是不可能的)
  • userAgent:自定义用户代理字符串 suggested in the docs
  • port:目标端口(默认为80/443)
  • encoding:响应编码(默认为utf8)

我没有测试这么多,但示例(见上文)应该仍然有效。

var http = require('http'),
https = require('https');

function searchWiki(site, search, callback, opts) {
if(typeof callback == 'object') {
opts = callback;
callback = null;
} else {
opts = opts || {};
}
// Build the required paths
var apiPath = (opts.apiBase || '/w/') + 'api.php';
var queryPath = apiPath + '?action=query&list=search&srsearch=' + encodeURIComponent(search) + '&srlimit=' + (opts.maxResults || 1) + '&format=json';
// Request options
var httpOpts = {
hostname: site,
port: (opts.port ? opts.port : (opts.ssl ? 443 : 80)),
method: 'GET',
path: queryPath,
agent: false
};
// Custom user agent
if(opts.userAgent) {
httpOpts.headers = {
'User-Agent': opts.userAgent
};
}
// Make the request
var req = (opts.ssl ? https : http).request(httpOpts, function(res) {
var msgBody = '';
res.setEncoding(opts.encoding || 'utf8');

res.on('data', function(chunk) {
msgBody += chunk;
});

res.on('end', function() {
// Parse response as JSON
var data;
try {
data = JSON.parse(msgBody);
} catch(err) {
(opts.error || function(){})(err);
return;
}
// Get all returned pages
var siteUrl = (opts.ssl ? 'https' : 'http') + '://' + site;
var titles = [], links = [];
for(var i = 0; i < data.query.search.length; i++) {
var title = data.query.search[i].title,
link = siteUrl + (opts.wikiBase || '/wiki/') + encodeURIComponent(title);
titles.push(title);
links.push(link);
}
if(!opts.maxResults) {
// Single result requested
if(data.query.search.length == 0) {
titles = links = null;
} else {
titles = titles[0];
links = links[0];
}
}
// Call the callback
(callback || opts.success || function(){})(titles, links);
});
});
req.on('error', function(err) {
(opts.error || function(){})(err);
});
req.end();
}

关于javascript - 搜索媒体维基,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19892179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com