爬虫篇 |
您所在的位置:网站首页 › 如何制作网页游戏辅助工具 › 爬虫篇 |
接到需求,要求要将几个好玩的网页版本的单机小游戏。采集到我们的服务器中。 这里以 水枪射手 这个游戏举例 http://m.7k7k.com/player/198961.htm?uc_biz_str=qk_enable_gesture%3afalse|OPT%3ABACK_BTN_STYLE%400%7COPT%3ATOOLBAR_STYLE%401 fiddler抓包工具: 将抓取的资源链接,复制到代码里。去下载 public static void main(String[] args) { //存到自己的文件夹位置 String localPath="D:/crawler_games/shoot/"; //pre_url 这个参数是:用于存文件夹时候,去掉链接的前面这一串路径 String pre_url="http://flash.7k7k.com/cms/cms10/20200116/1213129483/01/"; String list[]={ //"这里输入抓包的所有代码 " "http://flash.7k7k.com/cms/cms10/20200116/1213129483/01/gameIndex.html", "http://flash.7k7k.com/cms/cms10/20200116/1213129483/01/h5api-interface.php", "http://flash.7k7k.com/cms/cms10/20200116/1213129483/01/index.js", "http://flash.7k7k.com/cms/cms10/20200116/1213129483/01/libs/laya.core.js", "http://flash.7k7k.com/cms/cms10/20200116/1213129483/01/libs/laya.ui.js", "http://flash.7k7k.com/cms/cms10/20200116/1213129483/01/libs/laya.d3.js", "http://flash.7k7k.com/cms/cms10/20200116/1213129483/01/libs/laya.physics3D.js", "http://flash.7k7k.com/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 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、执行完后,就可以看到采集的文件资源了(从自己指定的目录里查看) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |