本文介绍了查询Stackoverflow API时如何调整返回的结果数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Stackoverflow JSON API来检索标记有给定标签的问题.

I'm using the Stackoverflow JSON API to retrieve questions marked with a given tag.

我有一个用Java编写的小程序,用于检索标有"Java"标记的问题.

I have this small program in Java which retrieves questions marked with the "Java" tag.

public static void main(String[] args) throws Exception
{
    String urlString = "https://api.stackexchange.com/2.1/questions?order=desc&sort=votes&tagged=java&site=stackoverflow";

    URL url = new URL( urlString );

    BufferedReader reader = null;

    StringBuffer buffer = new StringBuffer();
    try
    {
        URLConnection connection = url.openConnection();

        InputStream isConn = connection.getInputStream();

        reader = new BufferedReader( new InputStreamReader( new GZIPInputStream( isConn ) ) );

        String inputLine;

        while (( inputLine = reader.readLine() ) != null)
        {
            buffer.append( inputLine );
        }
    }
    finally
    {
        if (reader != null)
        {
            reader.close();
        }
    }

    JSONObject jsonObject = new JSONObject( buffer.toString() );

    JSONArray jsonArray = jsonObject.getJSONArray( "items" );

    System.out.println( buffer );
    System.out.println( jsonArray.length() );
}

我的问题是它只返回30个问题.由于我的目标是建立数据集以进行进一步的文本分析,因此我需要访问的方式不止30个问题.

My problem is that it returns only 30 questions. Since my goal is to build a dataset for further textual analysis, I need to access way more than just 30 questions.

是否可以调整返回列表的大小?

Is there a way to adjust the size of the returned list?

如果没有,如何解决这种情况?

If not, how can I workaround this situation?

推荐答案

请注意返回的JSON中的has_more属性,这表明有更多结果可用.您可以使用url中的pagepagesize参数来分页显示这些结果.我预见的问题是,考虑到它将遍历所有Java问题,因此代码将拉动大量问题,因此您可能需要添加一个在一定数量的页面处停止的条件.这是一个简单的示例:

Notice the has_more property in the returned JSON, this indicates that more results are available. You can page through these results using the page and pagesize parameters in the url. The issue I foresee is the code will be pulling a large number of questions considering it will iterate through all java questions, so you may want to add a conditional that stops at a certain number of pages. Here is a quick example:

public static void main(String[] args) throws Exception {

    BufferedReader reader = null;
    int page = 1;
    JSONObject jsonObject = null;
    try {
        while (jsonObject == null || jsonObject.getBoolean("has_more")) {
            String urlString = "https://api.stackexchange.com/2.1/questions?order=desc&sort=votes&tagged=java&site=stackoverflow&pagesize=100";
            urlString += "&page=" + page++;
            URL url = new URL(urlString);
            URLConnection connection = url.openConnection();

            InputStream isConn = connection.getInputStream();
            StringBuffer buffer = new StringBuffer();
            reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(isConn)));

            String inputLine;

            while ((inputLine = reader.readLine()) != null) {
                buffer.append(inputLine);
            }

            jsonObject = new JSONObject(buffer.toString());
            JSONArray jsonArray = jsonObject.getJSONArray("items");

            System.out.println(buffer);
            System.out.println(jsonArray.length());
        }
    } finally {
        if (reader != null) {
            reader.close();
        }
    }

}

这篇关于查询Stackoverflow API时如何调整返回的结果数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 19:00