接到需求,要求要将几个好玩的网页版本的单机小游戏。采集到我们的服务器中。

这里以 水枪射手 这个游戏举例
http://m.7k7k/player/198961.htm?uc_biz_str=qk_enable_gesture%3afalse|OPT%3ABACK_BTN_STYLE%400%7COPT%3ATOOLBAR_STYLE%401

1、首先准备好抓包工具或者直接在用浏览器自带的抓包,推荐用抓包工具可以批量复制抓到的资源链接。
如chrome浏览器:

fiddler抓包工具:

2、抓包工具准备好后,然后开始玩一遍游戏,在玩游戏的时候通过抓包工具,抓取游戏的资源链接。 需要注意的是,有的游戏,在刚进去的时候会吧所有的资源链接都加载出来,而有的游戏则需要一边玩,一边加载新的资源链接。

将抓取的资源链接,复制到代码里。去下载

 public static void main(String[] args) {
        //存到自己的文件夹位置
        String localPath="D:/crawler_games/shoot/";

        //pre_url 这个参数是:用于存文件夹时候,去掉链接的前面这一串路径
        String pre_url="http://flash.7k7k/cms/cms10/20200116/1213129483/01/";
        String list[]={

 				//"这里输入抓包的所有代码 "
                "http://flash.7k7k/cms/cms10/20200116/1213129483/01/gameIndex.html",
                "http://flash.7k7k/cms/cms10/20200116/1213129483/01/h5api-interface.php",
                "http://flash.7k7k/cms/cms10/20200116/1213129483/01/index.js",
                "http://flash.7k7k/cms/cms10/20200116/1213129483/01/libs/laya.core.js",
                "http://flash.7k7k/cms/cms10/20200116/1213129483/01/libs/laya.ui.js",
                "http://flash.7k7k/cms/cms10/20200116/1213129483/01/libs/laya.d3.js",
                "http://flash.7k7k/cms/cms10/20200116/1213129483/01/libs/laya.physics3D.js",
                "http://flash.7k7k/cms/cms10/20200116/1213129483/01/js/bundle.js",
                //"这里输入抓包的所有代码...上面这些只是小部分示例"


        };
        try {
            for (String s : list) {
                String urlName = s.replace(pre_url,"");
                String path=localPath+urlName;
                downloadNet(s,path,localPath,pre_url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //下载资源文件的方法
    private static void downloadNet(String crawlerUrl,String path,String localPath,String pre_url) throws Exception {
        if (!crawlerUrl.contains(pre_url)){
            return;
        }

        //这里的将首页资源,刚换名字
        if(path.contains("gameIndex.html")){
            path=localPath+"\\index.html";
        }
        System.out.println("完成 :" +path);
        // 下载网络文件
        int bytesum = 0;
        int byteread = 0;

        URL url = new URL(crawlerUrl);
        String[] split = path.split("\\/");
        System.out.println("长度"+split.length);
        for (int i = 1; i < split.length; i++) {
            String everyPath="";
            for (int j = 0; j < i; j++) {
                everyPath+=split[j]+"/";
                File f = new File(everyPath);
                //没有目录,就创建目录
                if (!f.exists()) {
                    try {
                        f.mkdirs();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();


            FileOutputStream fs = new FileOutputStream(path);

            byte[] buffer = new byte[1204];
            int length;
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3、执行main方法采集:

4、执行完后,就可以看到采集的文件资源了(从自己指定的目录里查看)

5、通过本地的环境的nginx就可以访问自己的抓的小游戏了, nginx做个简单的配置,配置静态资源访问即可(注意:游戏的资源后缀比较多、且少见,按后缀配置静态资源访问的时候,一定保证这些资源都可以正常访问):

6、一切准备好后,可以访问自己本地的游戏链接了

更多推荐

爬虫篇——采集单机游戏(网页游戏),爬取小游戏