首页 > 解决方案 > Scrapinghub/Splash 网站页面获取时间随着并行线程呈指数增长

问题描述

在我的试验中,我用 50 个并行线程点击了启动实例。每个线程都会获取 URL 的页面源。我的启动实例默认槽值为 50。这里,网站获取时间随着并行线程的数量呈指数增长。我可以获得 50 个 URL 的完美 HTML 源代码。但是从第 1 个 URL 到第 50 个 URL,时间分别从 2 秒增加到 45 秒。请帮助我减少获取页面源的时间。

我的示例 java 代码是

public class SplashThread implements Runnable {

private static final String splash ="http://localhost:8050/execute";

private String script = URLEncoder.encode("function main(splash, args)  \n" +
        "splash:go(args.url)\n"+
        "  splash.images_enabled = false\n" +
        "  splash:on_request(function(request)\n" +
        "    if string.find(request.url, \".css\")~= nil then\n" +
        "        request.abort()\n" +
        "    end\n" +
        "end)\n" +
        "local html = splash:html()\n" +
        "return  html\n"+
        "end","UTF-8");

private String url =null;

public SplashThread(String url) throws UnsupportedEncodingException {
    this.url = url;

}
@Override
public void run() {
    HttpClientUtil clientUtil =null;
    JSONObject json =null;
    try {
        Properties queryParms = new Properties();
        queryParms.put("url",url);
        queryParms.put("timeout","85.0");
        queryParms.put("lua_source",script);

        clientUtil = new HttpClientUtil(-1,false);
        HttpResponse response = clientUtil.doGet(splash,queryParms,null);
        String resp = ScrapyUtil.getResponseString(response,"UTF-8");


    }
    catch (Exception e){
        e.printStackTrace();
        System.out.println("JSN}ON :: "+json);
    }
    finally {
        if(clientUtil!=null){
            try {
                clientUtil.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

}

我正在使用 ScheduledExecutorService 调度这个可运行对象的 50 个线程。

如果我一个一个地修改页面源,它​​将完美地工作。但我需要同时进行。

标签: webkitqtwebkitscrapy-splashscrapinghubsplash-js-render

解决方案


推荐阅读