如何通过一次调用从不同对象获取信息



我需要一个包含3个字段的标签列表:

  1. 标签名称

  2. 标签description

  3. counter_of_posts。

由于counter_of_posts是标记对象的字段,而tag_description(摘录(是tag_wiki的字段,我如何通过一次调用获得所需信息?

这是不可能的-您至少需要2个调用(如果您有20个以上的标记,则需要更多调用(。一个调用/tags/{tags}/info?site={site}以获取标签的名称(已给定(和计数器,另一个调用为/tags/{tags}/wikis?site={site}。当然,你可以应用你想要的任何过滤器,并更改网站名称和标签名称。下面是一个JavaScript示例:

(async function() {
const key = '3loXx7CAr2AvrMaHBj6GxQ(('; // not necessary, but it increases daily API quota from 300 to 10000
const sitename = 'stackoverflow'; // default, change it to whatever you want
const tags = 'php;javascript;java;jquery;perl;python'; // semicolon-separated, must be =<20
const tagApiUrl = 'https://api.stackexchange.com/2.2/tags/';
const tagInfoFilter = '!-.G.68pp778y';
const tagWikisFilter = '!*Ly1)NvM)n91RtK*';
// First API call: get tag's info
const callTagInfo = await fetch(`${tagApiUrl}${tags}/info?site=${sitename}&filter=${tagInfoFilter}&key=${key}`);
const data_counter = await callTagInfo.json();
// Second API call: get tag's excerpt
const callTagWikis = await fetch(`${tagApiUrl}${tags}/wikis?site=${sitename}&filter=${tagWikisFilter}&key=${key}`);
const data_excerpt = await callTagWikis.json();
for (let i = 0; i < data_counter.items.length; i++) {
const table = document.querySelector('table');
const html = `
<tr>
<td>${data_counter.items[i].name}</td>
<td>${data_excerpt.items.find(name => name.tag_name === data_counter.items[i].name).excerpt}</td>
<td>${data_counter.items[i].count}</td>
</tr>`;
table.insertAdjacentHTML('beforeend', html);
}
console.log('API Quota remaining:', data_excerpt.quota_remaining);
})();
<link rel="stylesheet" href="https://unpkg.com/@stackoverflow/stacks/dist/css/stacks.min.css">
<table class="s-table">
<tbody>
<tr>
<th>Tag Name</th>
<th>Excerpt</th>
<th>Number of posts</th>
</tr>
</tbody>
</table>

相关内容

  • 没有找到相关文章

最新更新