Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在JavaSwing中使用jsoup在web抓取中分页_Java_Swing_Pagination_Jsoup - Fatal编程技术网

在JavaSwing中使用jsoup在web抓取中分页

在JavaSwing中使用jsoup在web抓取中分页,java,swing,pagination,jsoup,Java,Swing,Pagination,Jsoup,这是显示数据。但是有分页。如何获取接下来五页的数据?幸运的是,正如您在下面的代码块中所看到的,我已经实现了您的目标。如果您不确定发生了什么,我会添加一些注释,希望能够描述每个步骤 我尝试过使用站点的分页设置,但它们似乎只允许每个请求增加5个结果,因此没有太大的余地,并且您需要通过起始点,然后它才能检索接下来的5个结果 因此,我必须将它包含在一个fori循环32次的中。这等于158学校,除以5等于31.6或四舍五入32当然,如果您只需要第一个5页面,您可以将循环更改为只循环5次 不管怎么说,还是要

这是显示数据。但是有分页。如何获取接下来五页的数据?

幸运的是,正如您在下面的代码块中所看到的,我已经实现了您的目标。如果您不确定发生了什么,我会添加一些注释,希望能够描述每个步骤

我尝试过使用站点的分页设置,但它们似乎只允许每个请求增加5个结果,因此没有太大的余地,并且您需要通过起始点,然后它才能检索接下来的5个结果

因此,我必须将它包含在一个
fori
循环
32次的
中。这等于
158
学校,除以
5
等于
31.6
或四舍五入
32
当然,如果您只需要第一个
5
页面,您可以将循环更改为只循环
5次

不管怎么说,还是要多汁一点

private void EducationWorld_Webscrap_jButtonActionPerformed(java.awt.event.ActionEvent evt)
{                                                                
     try
     {
         Document doc=Jsoup.connect("http://www.educationworld.in/institution/mumbai/schools").userAgent("Mozilla/17.0").get();
         Elements  links=doc.select("div.instnm.litblue_bg");
         StringBuilder sb1 = new StringBuilder ();
         links.stream().forEach(e->sb1.append(e.text()).append(System.getProperty("line.separator")));
         jTextArea1.setText(sb1.toString());
     }
     catch(Exception e)
     {
         JOptionPane.showMessageDialog(null, e);
     }
} 
import org.jsoup.jsoup;
导入org.jsoup.nodes.Document;
导入org.jsoup.select.Elements;
导入java.io.*;
导入java.net。*;
公共类循环
{
公共静态void main(字符串[]args)
{
最终StringBuilder sb1=新StringBuilder();
BufferedReader BufferedReader=null;
OutputStream OutputStream=null;
尝试
{
//参数分页计数
int startCount=0;
int limitCount=5;
//循环32次,158所学校/5(分页数量)
对于(int i=0;i<32;i++)
{
//打开与提供的URL的连接
最终URL连接URL连接=新URL(“http://www.educationworld.in/institution/mumbai/schools”).openConnection();
//告诉URL我们正在发送输出
urlConnection.setDoOutput(true);
//我们将要写入URL的流
outputStream=urlConnection.getOutputStream();
//分页的设置参数
最后一个字符串params=“qstart=“+startCount+”&limit=“+limitCount;
//获取分页参数的字节数
最终字节[]输出字节=参数getBytes(“UTF-8”);
//将字节写入URL
outputStream.write(输出字节);
//获取并读取URL响应
bufferedReader=新的bufferedReader(新的InputStreamReader(urlConnection.getInputStream());
StringBuilder响应=新建StringBuilder();
字符串输入线;
//循环响应并读取将其附加到StringBuilder的每一行
而((inputLine=bufferedReader.readLine())!=null)
{
追加(inputLine);
}
//与以前一样,只使用字符串
最终文档doc=Jsoup.parse(response.toString());
元素链接=文件选择(“div.instnm.litblue_bg”);
links.forEach(e->sb1.append(e.text()).append(System.getProperty(“line.separator”));
//增加分页参数
startCount+=5;
limitCount+=5;
}
System.out.println(sb1.toString());
jTextArea1.setText(sb1.toString());
}
捕获(例外e)
{
e、 printStackTrace();
}
最后
{
尝试
{
//关闭bufferedReader
if(bufferedReader!=null)
{
bufferedReader.close();
}
//关闭输出流
if(outputStream!=null)
{
outputStream.close();
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
}

希望这能帮助你得到你想要的结果,如果你需要任何东西,尽管问吧

这将显示控制台输出中的所有数据。但是当我写jTextArea1.setText(sb1.toString())时;代替System.out.println(sb1.toString());它只显示3所学校的名称。修改了
StringBuilder
setText(sb1.toString())
请参阅更新的答案。现在应该将所有学校放在您的文本区域中。@D.Rudra更新了吗?如果你能让我知道那太好了!谢谢汤姆C先生。现在所有学校的名字都显示出来了。加载数据需要2分钟,但工作绝对正常。如果有助于解决问题,请联系我们。
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.*;

public class Loop
{
    public static void main( String[] args )
    {
        final StringBuilder sb1 = new StringBuilder();
        BufferedReader bufferedReader = null;
        OutputStream outputStream = null;

        try
        {
            // Parameter pagination counts
            int startCount = 0;
            int limitCount = 5;

            // Loop 32 times, 158 schools / 5 (pagination amount)
            for( int i = 0; i < 32; i++ )
            {
                // Open a connection to the supplied URL
                final URLConnection urlConnection = new URL( "http://www.educationworld.in/institution/mumbai/schools" ).openConnection();
                // Tell the URL we are sending output
                urlConnection.setDoOutput( true );
                // The stream we will be writing to the URL
                outputStream = urlConnection.getOutputStream();

                // Setup parameters for pagination
                final String params = "qstart=" + startCount + "&limit=" + limitCount;
                // Get the bytes of the pagination parameters
                final byte[] outputInBytes = params.getBytes( "UTF-8" );
                // Write the bytes to the URL
                outputStream.write( outputInBytes );

                // Get and read the URL response
                bufferedReader = new BufferedReader( new InputStreamReader( urlConnection.getInputStream() ) );
                StringBuilder response = new StringBuilder();
                String inputLine;

                // Loop over the response and read each line appending it to the StringBuilder
                while( (inputLine = bufferedReader.readLine()) != null )
                {
                    response.append( inputLine );
                }

                // Do the same as before just with a String instead
                final Document doc = Jsoup.parse( response.toString() );
                Elements links = doc.select( "div.instnm.litblue_bg" );
                links.forEach( e -> sb1.append( e.text() ).append( System.getProperty( "line.separator" ) ) );

                // Increment the pagination parameters
                startCount += 5;
                limitCount += 5;
            }

            System.out.println( sb1.toString() );
            jTextArea1.setText(sb1.toString());
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                // Close the bufferedReader
                if( bufferedReader != null )
                {
                    bufferedReader.close();
                }

                // Close the outputStream
                if( outputStream != null )
                {
                    outputStream.close();
                }
            }
            catch( IOException e )
            {
                e.printStackTrace();
            }
        }
    }
}