使用Stack Exchange API获取帖子标签

时间:2019-06-17 07:36:33

标签: sql api stackexchange-api

基于来自Stack Exchange数据浏览器(SEDE)的查询:

select Tags.TagName, Count(Tags.TagName) as TagCount from Posts  
      join PostTags on Posts.Id = PostTags.PostID
      join Tags on PostTags.TagID = Tags.ID
      where Posts.Tags like '%<keras>%'
      group by Tags.TagName
      order by TagCount desc

有一个名为PostTags的表,其中包含帖子中的标签。

通过使用Stack Exchange API,我可以获得帖子。 但是如何在帖子中获取标签?

1 个答案:

答案 0 :(得分:2)

问题尚不清楚。但是,如果您要在the Stack Exchange API中重复该SEDE查询,请知道这是可能的,但并非那么简单。未为临时数据摘要设置该API。

要获得问题中存在的标签的摘要,您还必须:

  1. 当心pagingquota limitations。当前有17699 questions tagged keras。那就是177个API 页面,或您日常API配额的很大一部分。
  2. 强烈建议您注册并使用an API key。这样您的配额将从300增加到10,000。
  3. 获取所有标记为keras的问题。
  4. 浏览结果,确定使用哪些标签以及使用频率。
  5. 获取最后一页后,请进行最后的处理和显示。

此功能齐全的演示说明了过程(但限制为2页结果,以避免耗尽演示的配额)

var srchTag     = "keras";
const cYourKey  = ""; //  "&key=3TIJXXXXXXXXXXXXXZB0AA((";  // key is optional but very recommended
const cMaxPages = 2;  //  WARNING:  Only set this higher once you are sure the code is as you want it.
var tagStats    = {};

getPageOfQuestions (1);
function getPageOfQuestions (pageNum) {
    fetch (
        `https://api.stackexchange.com/2.2/questions?tagged=${srchTag}&page=${pageNum}&pagesize=100&sort=creation&order=asc&site=stackoverflow&filter=!-W2eZY)NCj(NnIzhnVP6${cYourKey}`
    )
    .then (function (response) {
        if (response.ok)  return response.json();
        throw new Error (`API problem: Error ${response.status}, ${response.statusText}`);
    } )
    .then (function (rJsn) {
        if (rJsn.backoff  ||  rJsn.error_id) {
            throw new Error (`From API: Backoff ${rJsn.backoff}, Error: ${rJsn.error_id}, ${rJsn.error_message}, ${rJsn.error_name}`);
        }
        console.log (`Quota remaining: ${rJsn.quota_remaining}; Items found: ${rJsn.items.length}`);

        let allDone     = ! rJsn.has_more;
        let zQuestions  = rJsn.items;
        for (let qstn of zQuestions) {
            for (let zTag of qstn.tags) {
                let tagStat     = (tagStats[zTag] || 0) + 1;
                tagStats[zTag]  = tagStat;
            }
        }
        if (rJsn.has_more) {
            let newPageNm  = rJsn.page + 1;
            if (newPageNm <= cMaxPages)
                getPageOfQuestions (newPageNm);
            else
                allDone = true;
        }
        if (allDone) {
            //--- Do final processing here...
            console.log ("IMPORTANT:  Due to current limitations of Stack Overflow Snippets, only the 1st 40 lines are displayed."); // prod code won't need that limit.
            let J = 1;
            let tagsByPop = Object.keys (tagStats).sort ( (a, b) => (tagStats[b] - tagStats[a]) );
            for (let zTag of tagsByPop) {
                console.log ( (zTag + ":").padEnd(35),  tagStats[zTag]);   //  35 is max tagName length.
                if (J++ >= 40)  break;
            }
        }
    } );
};
.as-console-wrapper { max-height: 100% !important; top: 0; }