Java 爬虫解决readline 读取缓慢问题

传统的使用URLconnection做连接之后通常使用in.readline()来获取网页代码。显而易见的是这种方法效率奇低。我简单的做了一下比较

    Python  time:0.205786745268 s =205ms
    传统 readline() time: 1975ms
    charBuffer time: 265 ms
try {
                    // 将string转成url对象
                    URL realUrl = new URL(surl);
                    // 初始化一个链接到那个url的连接
                    URLConnection connection = realUrl.openConnection();
                    // 开始实际的连接
                    connection.connect();
                    // 初始化 BufferedReader输入流来读取URL的响应
                    in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    // 用来临时存储抓取到的每一行的数据
                    String line;
                    long startMili=System.currentTimeMillis();// 当前时间对应的毫秒数
//                  while ((line = in.readLine()) != null) {
//                      // 遍历抓取到的每一行并将其存储到result里面
//                      Log.i(">>line",line);
//                      result += line + "\n";
//                  }
                    CharBuffer bos = CharBuffer.allocate(20480);
                    int read = 0;
                    StringBuilder builder = new StringBuilder();
                    try {
                        while (in.read(bos) != -1) {
                            bos.flip();
                            builder.append(bos.toString());
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    //System.out.println(builder.toString());
                    result=builder.toString();
                    //写入byte数组中。再依次读取出来即可。
                    //System.out.print(result);
                    long endMili=System.currentTimeMillis();// 当前时间对应的毫秒数

                    Log.i(">>time1","总耗时为:"+(endMili-startMili)+"毫秒");
                } catch (Exception e) {
                    Log.i(">>>parsing","发送GET请求出现异常!" + e);
                    e.printStackTrace();
                }
                // 使用finally来关闭输入流
                finally {
                    try {
                        if (in != null) {
                            in.close();
                        }
                    } catch (Exception e2) {
                        e2.printStackTrace();
                    }
                    finally {
                        TAG=true;
                    }
                }
            }

孰优孰劣简单易得,在能够大致预见网页大小的时候,很明显使用charBuffer效率高出了10多倍。

github 地址 https://github.com/panyunyi97/CUFE_TRIP 欢迎观光&& star

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTTP 307状态码表示请求重定向,客户端需要重新发送请求到另一个地址。在爬虫过程中,可能会遇到307状态码,这时候需要对请求进行重定向处理。 以下是一个Java代码示例,用于解决爬虫307问题: ```java import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class Spider { public static void main(String[] args) { String url = "https://www.example.com"; String result = ""; try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setInstanceFollowRedirects(false); // 禁止自动重定向 connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == 307) { String redirectUrl = connection.getHeaderField("Location"); // 获取重定向地址 connection = (HttpURLConnection) new URL(redirectUrl).openConnection(); connection.connect(); InputStream inputStream = connection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = bufferedReader.readLine()) != null) { result += line; } } else { InputStream inputStream = connection.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = bufferedReader.readLine()) != null) { result += line; } } } catch (Exception e) { e.printStackTrace(); } System.out.println(result); } } ``` 该代码通过禁止自动重定向,获取重定向地址,重新发送请求到重定向地址,并获取响应数据解决爬虫307问题。需要注意的是,重定向可能会出现循环重定向的情况,需要进行判断和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值