如何通過jsoup網路爬蟲工具爬取網頁資料,並通過jxl工具匯出到excel
1:閒話少說,直接看需求:
抓取的url:http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=.
參考的資料:http://blog.csdn.net/lmj623565791/article/details/23272657需求:抓取指定url,共n頁資訊(即需要分析內部連結),將頁面上的停車資訊抓取出來(即需要分析停車資訊的特徵),並匯入到excel中.
問題分析:因為頁面上存在相同的停車場資訊(停車場名稱,停車場地址和停車場車位個數相同),那麼匯入到excel中也存在相同資訊,
客戶需求是相同的資訊只需保留一項,所以在匯入前需要過濾.
2:看完需求看要抓取的頁面:
3.分析網頁原始碼:
4.分析關於訪問網頁設計到的一些重要資訊<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <!-- saved from url=(0074)http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page= --> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link href="./全市停車場-上海停車網_files/base.css" rel="stylesheet" type="text/css"> <link href="./全市停車場-上海停車網_files/reset.css" rel="stylesheet" type="text/css"> <!--[if IE 6]> <script type="text/javascript" src="http://www.shparking.cn/Scripts/DD_belatedPNG.js" ></script> <script type="text/javascript"> DD_belatedPNG.fix('body,img,div,ul,li,dl,dd,dt,a,input,span');</script> <![endif]--> <script type="text/javascript" src="./全市停車場-上海停車網_files/jquery-1.7.1.min.js"></script> <script type="text/javascript" src="./全市停車場-上海停車網_files/index.js"></script> <link rel="stylesheet" href="http://www.shparking.cn/colorbox/colorbox.css"> <script type="text/javascript" src="./全市停車場-上海停車網_files/lrtk.js"></script> <script src="./全市停車場-上海停車網_files/jquery.colorbox.js"></script> <script> $(document).ready(function(){ $(".reg").colorbox({iframe:true, innerWidth:496, innerHeight:411}); $(".forgetpwd").colorbox({iframe:true, innerWidth:496, innerHeight:242}); $(".mylogin").colorbox({iframe:true, innerWidth:496, innerHeight:266}); }); </script> <title>全市停車場-上海停車網</title> <script> $(function(){ $("#btn-search").click(function(){ var key = $("#txt-key").val(); location.href='http://www.shparking.cn/index.php/welcome/municipal_parking?key=' + key; }); }); </script> </head> <body><div id="cboxOverlay" style="display: none;"></div><div id="colorbox" class="" style="padding-bottom: 32px; padding-right: 0px; display: none;"><div id="cboxWrapper"><div><div id="cboxTopLeft" style="float: left;"></div><div id="cboxTopCenter" style="float: left;"></div><div id="cboxTopRight" style="float: left;"></div></div><div style="clear: left;"><div id="cboxMiddleLeft" style="float: left;"></div><div id="cboxContent" style="float: left;"><div id="cboxLoadedContent" style="width: 0px; height: 0px; overflow: hidden; float: left;"></div><div id="cboxLoadingOverlay" style="float: left;"></div><div id="cboxLoadingGraphic" style="float: left;"></div><div id="cboxTitle" style="float: left;"></div><div id="cboxCurrent" style="float: left;"></div><div id="cboxNext" style="float: left;"></div><div id="cboxPrevious" style="float: left;"></div><div id="cboxSlideshow" style="float: left;"></div><div id="cboxClose" style="float: left;"></div></div><div id="cboxMiddleRight" style="float: left;"></div></div><div style="clear: left;"><div id="cboxBottomLeft" style="float: left;"></div><div id="cboxBottomCenter" style="float: left;"></div><div id="cboxBottomRight" style="float: left;"></div></div></div><div style="position: absolute; width: 9999px; visibility: hidden; display: none;"></div></div> <script> $(function(){ $('#username').focus(function(){ if($(this).val() == '請輸入使用者名稱'){ $(this).val(''); } }); $('#username').blur(function(){ if($(this).val() == ''){ $(this).val('請輸入使用者名稱'); } }); $.ajax({ type: "post", url: "http://www.shparking.cn/index.php/member/getUser", success: function(result) { var user; eval("user="+result); if(user != null){ if(user.usertype==2){ $("#member-type").html('<a>工作人員</a>'); } $('#logout-box').show(); }else{ $('#signinform-box').show(); } }, error: function() { $('#signinform-box').show(); } }); }); </script> <div class="top"> <div class="main"> <dl id="signinform-box" style=""> <dt><img src="./全市停車場-上海停車網_files/ren.png" width="18" height="19"></dt> <dd><a class="reg cboxElement" href="http://www.shparking.cn/index.php/member/reg">註冊</a></dd> <span> <dd class="login"><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=#">登入</a></dd> <div class="loginInfo"> <form action="http://www.shparking.cn/index.php/member/signin" method="post" name="signinform" id="signinform"> <input type="hidden" name="act" value="signin"> <ul> <li> <input name="username" id="username" type="text"> </li> <li> <input name="password" id="password" type="password"> </li> <li class="submit"> <input name="" type="image" src="./全市停車場-上海停車網_files/denlu.jpg"> <a href="http://www.shparking.cn/index.php/member/forgetpassword" class="forgetpwd cboxElement">忘記密碼</a> </li> </ul> </form> </div> </span> </dl> <dl id="logout-box" style="display:none"> <dt><img src="./全市停車場-上海停車網_files/ren.png" width="18" height="19"></dt> <dd id="member-type"><a href="http://www.shparking.cn/index.php/member/main">會員中心</a></dd> <dd><a href="http://www.shparking.cn/index.php/member/logout">退出</a></dd> </dl> </div> </div> <div class="menu"> <div class="main"> <div class="logo"><img src="./全市停車場-上海停車網_files/logo.png" width="168" height="55"></div> <div class="info"> <ul> <li class="index" style="cursor: pointer;"><a href="http://www.shparking.cn/">首頁</a></li> <li class="" style="cursor: pointer;"><a href="http://www.shparking.cn/index.php/welcome/travel">出行寶典</a></li> <li class="" style="cursor: pointer;"><a href="http://www.shparking.cn/index.php/welcome/onlineservice">網上辦事</a></li> <li class="" style="cursor: pointer;"><a href="http://www.shparking.cn/index.php/welcome/news">資訊中心</a></li> <li class="" style="cursor: pointer;"><a href="http://www.shparking.cn/index.php/welcome/policy">政策法規</a></li> <li class="" style="cursor: pointer;"><a href="http://www.shparking.cn/index.php/welcome/companylist">會員中心</a></li> <li class="" style="cursor: pointer;"><a href="http://www.shparking.cn/index.php/welcome/aboutass">關於協會</a></li> </ul> </div> </div> </div> <div class="main"> <div class="NlistL"> <dl class="aboutnews"> <dt>查詢<span>SEARCH</span></dt> <dd><input id="txt-key" type="text" value=""><input id="btn-search" type="button" value="查詢"></dd> </dl> <div class="ad"> <div style="float:left; width:240px; height:160px; overflow:hidden; background:url(http://www.shparking.cn/uppic/municipal_parking.png) no-repeat 0px 0px;"> <div style="float:left; width:100px; height:35px; margin:28px 0px 0px 23px; cursor:pointer" onclick="location.href='http://www.shparking.cn/index.php/welcome/municipal_parking'"></div> <div style="float:left; width:100px; height:35px; margin:28px 0px 0px 23px; cursor:pointer" onclick="location.href='http://www.shparking.cn/index.php/welcome/dl_parking'"></div> </div> </div> <div class="ad" style="height:5px; clear:both"> </div> <dl class="aboutnews"> <dt>相關新聞<span>NEW</span></dt> <dd><a href="http://www.shparking.cn/index.php/welcome/newsinfo/6/6/1771">上海停車資訊(第140期...</a></dd> <dd><a href="http://www.shparking.cn/index.php/welcome/newsinfo/6/6/1616">關於開展“同心共築中...</a></dd> <dd><a href="http://www.shparking.cn/index.php/welcome/newsinfo/6/6/1791">家門口的停車場到底該...</a></dd> <dd><a href="http://www.shparking.cn/index.php/welcome/newsinfo/6/6/1790">春運首日虹橋機場已難...</a></dd> <dd><a href="http://www.shparking.cn/index.php/welcome/newsinfo/6/6/1789">上海靜安交警“先禮後...</a></dd> <dd><a href="http://www.shparking.cn/index.php/welcome/newsinfo/6/6/1788">廢舊停車場改建成188間...</a></dd> </dl> </div> <div class="NlistR"> <div class="nav">當前狀態:<a href="http://www.shparking.cn/">上海停車網</a> > 全市停車場庫</div> <ul class="list"> <li style="background:none; background-color:#f1f1f1;"> <span style="float:left; width:320px; font-size:13px;" align="left">停車場名稱</span> <span style="float:left; width:230px; font-size:13px;" align="left">地址</span> <span style="float:left; width:60px; font-size:13px;" align="center">泊位數</span> <span style="float:left; width:90px; font-size:13px;" align="center">價格</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海銀河賓館有限公司" align="left">上海銀河賓館有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="中山西路888號" align="left">中山西路888號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">88</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">10</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海揚子江大酒店有限公司" align="left">上海揚子江大酒店有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="延安西路2099號" align="left">延安西路2099號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">46</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">20</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海北聖投資管理有限公司" align="left">上海北聖投資管理有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="黃楊路18號" align="left">黃楊路18號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">71</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">7</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海北聖投資管理有限公司" align="left">上海北聖投資管理有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="黃楊路18號" align="left">黃楊路18號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">71</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">4</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海東方明珠物產管理有限公司" align="left">上海東方明珠物產管理有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="東方路1866-1882號" align="left">東方路1866-1882號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">15</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">5</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海百聯汽車經營服務發展有限公司" align="left">上海百聯汽車經營服務發展有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="共和新路3550號" align="left">共和新路3550號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">70</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">5</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海江海建設開發公司" align="left">上海江海建設開發公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="張楊路818號" align="left">張楊路818號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">149</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">9</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海江海建設開發公司" align="left">上海江海建設開發公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="張楊路818號" align="left">張楊路818號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">149</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">5</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海奧凱酒店經營管理有限公司" align="left">上海奧凱酒店經營管理有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="石龍路569號" align="left">石龍路569號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">31</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">4</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海靜工巨集林投資發展有限公司" align="left">上海靜工巨集林投資發展有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="昌平路68號" align="left">昌平路68號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">0</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center"></span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海人民企業集團物業管理有限公司" align="left">上海人民企業集團物業管理有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="吳江路188號" align="left">吳江路188號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">0</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center"></span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海惠真醫藥有限公司" align="left">上海惠真醫藥有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="寶山區友誼路185號" align="left">寶山區友誼路185號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">105</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">3</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海惠真醫藥有限公司" align="left">上海惠真醫藥有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="寶山區友誼路185號" align="left">寶山區友誼路185號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">105</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">6</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海同樂坊物業管理有限公司" align="left">上海同樂坊物業管理有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="海防路537號9幢01室" align="left">海防路537號9幢01室</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">32</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">10</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海長翎管理諮詢有限公司" align="left">上海長翎管理諮詢有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="淮安路735號" align="left">淮安路735號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">21</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">6</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海香梅休閒健身俱樂部有限公司" align="left">上海香梅休閒健身俱樂部有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="浦東梅花路1號" align="left">浦東梅花路1號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">17</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">5</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="泊億管理諮詢(上海)有限公司虹口分公司" align="left">泊億管理諮詢(上海)有限公司虹口分公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="東寶興路777號" align="left">東寶興路777號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">80</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">8</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海金沙江大酒店有限公司" align="left">上海金沙江大酒店有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="怒江路257號" align="left">怒江路257號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">60</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">4</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海維思鼎文化發展有限公司" align="left">上海維思鼎文化發展有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="蘭溪路138號" align="left">蘭溪路138號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">32</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">6</span> </li> <li> <span style="float:left; width:320px; height:28px; font-size:13px; overflow:hidden" title="上海磊明工貿有限公司" align="left">上海磊明工貿有限公司</span> <span style="float:left; width:230px; height:28px; font-size:12px; overflow:hidden" title="華聯路130弄32號" align="left">華聯路130弄32號</span> <span style="float:left; width:60px; height:28px; font-size:12px; overflow:hidden" align="center">22</span> <span style="float:left; width:90px; height:28px; font-size:12px; overflow:hidden" align="center">4</span> </li> </ul> <ul class="page"> <li>1</li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=20">2</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=40">3</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=60">4</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=80">5</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=100">6</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=120">7</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=140">8</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=160">9</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=180">10</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=200">11</a></li><li><a href="http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3300">>|</a></li> </ul> </div> </div> <div class="clear"></div> <div class="footer"> <div class="main"> <div class="link"> <dl> <dt><img src="./全市停車場-上海停車網_files/linklogo.png" width="110" height="40"></dt> <dd><a href="http://www.jt.sh.cn/">上海市交通網</a></dd> <dd><a href="http://www.shygc.net/pageHome.do;jsessionid=60CFD91E9852E9DCC95C163CD759BB89?page=initList">上海市城市交通運輸管理處</a></dd> <dd><a href="http://www.shjtaq.com/main/index.asp">上海交通安全資訊網</a></dd> <dd><a href="http://www.shjpxh.org.cn/">上海市機動車駕駛員培訓行業網站</a></dd> <dd><a href="http://www.chinaparking.org/">中國停車網</a></dd> </dl> <div class="clear"></div> </div> </div> <div class="line"></div> <div class="main"><div class="copyright"><a href="http://www.shparking.cn/index.php/welcome/view/8/14/40">關於我們</a><a href="http://www.shparking.cn/index.php/welcome/view/8/14/41">聯絡我們</a><a href="http://www.shparking.cn/index.php/welcome/view/8/14/42">網站地圖</a> <script src="./全市停車場-上海停車網_files/stat.php" language="JavaScript"></script><script src="./全市停車場-上海停車網_files/core.php" charset="utf-8" type="text/javascript"></script><a href="http://www.cnzz.com/stat/website.php?web_id=5192959" target="_blank" title="站長統計">站長統計</a><br> <span>滬ICP備11029623號-2 © 2015 版權所有</span><span>主辦單位:上海市停車服務業行業協會 承辦單位:上海市中停車管理服務有限公司</span></div></div> </div> </body></html>
5.結合業務,建立要抓取的資料模型類
package com.ilucky.util.webspider; /** * 抓取的資料模型. * @author IluckySi * @since 20150210 */ public class DataModel { private String parkName;//停車場名稱. private String parkAddress;//停車場地址. private String parkCount;//停車場停車位個數. private String parkPrice;//停車場價格. public String getParkName() { return parkName; } public void setParkName(String parkName) { this.parkName = parkName; } public String getParkAddress() { return parkAddress; } public void setParkAddress(String parkAddress) { this.parkAddress = parkAddress; } public String getParkCount() { return parkCount; } public void setParkCount(String parkCount) { this.parkCount = parkCount; } public String getParkPrice() { return parkPrice; } public void setParkPrice(String parkPrice) { this.parkPrice = parkPrice; } public String toString() { return ("parkName = " + parkName + ", parkAddress = " + parkAddress + ", parkCount = " + parkCount + ", parkPrice = " + parkPrice); } }
6.抓取工具類,結合對抓取檔案的原始碼
package com.ilucky.util.webspider;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/**
* 抓取工具類,最重要的是如何獲取被抓取資料的特徵.
* 抓取的url:http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=.
* @author IluckySi
* @since 20150210
*/
public class WebSpiderUtil {
private String url;//抓取的url.
private List<DataModel> dataModelList = new ArrayList<DataModel>();//存放被抓取資料.
private Set<String> urlSet = new HashSet<String>();//存放內部url資料.
private Connection connection = null;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List<DataModel> getDataModelList() {
return dataModelList;
}
public void setDataModelList(List<DataModel> dataModelList) {
this.dataModelList = dataModelList;
}
public Set<String> getUrlSet() {
return urlSet;
}
public void setUrlSet(Set<String> urlSet) {
this.urlSet = urlSet;
}
/**
* 通過第三方類庫解析url對應的html資料,並根據特徵獲取需要的資料,
* 同時根據特徵獲取需要的內部url,並繼續解析內部url對應的html資料,
* 即通過遞迴的方式,完成對所有相關url對應的html資料的解析.
*/
public void caputerData() {
try {
if(url != null && !url.equals("")) {
System.out.println("正在抓取的頁面url = " + url);
connection = Jsoup.connect(url);
Document document = connection.timeout(100000).get();
//抓取當前頁面的內部url資料.
Elements resultUrls = document.getElementsByClass("page");
Elements links = null;
for (Element resultUrl : resultUrls) {
links = resultUrl.getElementsByTag("a");
}
//解析當前url對應的html資料.
Elements resultDatas = document.getElementsByClass("list");
for (Element resultData : resultDatas) {
Elements uls = resultData.getElementsByTag("ul");
for (Element ul : uls) {
Elements lis = ul.getElementsByTag("li");
for (int i = 1; lis != null && i < lis.size(); i++) {
Element li = lis.get(i);
Elements spans = li.getElementsByTag("span");
if(spans != null && spans.size() == 4) {
DataModel dataModel = new DataModel();
dataModel.setParkName(spans.get(0).text());
dataModel.setParkAddress(spans.get(1).text());
dataModel.setParkCount(spans.get(2).text());
dataModel.setParkPrice(spans.get(3).text());
dataModelList.add(dataModel);
}
}
}
}
//難點:獲取需要抓取的下一個url,通過Set集合儲存已經解析過的url,防止重複解析.
urlSet.add(url);
boolean flag = false;
for(int i = 0; links != null && i < links.size(); i++) {
String link = links.get(i).attr("href");
if(!urlSet.contains(link)) {
url = link;
flag = true;
break;
}
}
if(flag) {
caputerData();
}
}
} catch (Exception e) {
System.out.println("抓取資料出現異常: url = " + url + ", e = " + e);
}
}
}
7.再看測試類
package com.ilucky.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.ilucky.util.jxl.JxlUtil;
import com.ilucky.util.webspider.DataModel;
import com.ilucky.util.webspider.WebSpiderUtil;
/**
* 抓取的url:http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=.
* 參考的資料:http://blog.csdn.net/lmj623565791/article/details/23272657
* 需求:抓取指定url,共n頁資訊(即需要分析內部連結),將頁面上的停車資訊抓取出來(即需要分析停車資訊的特徵),並匯入到excel中.
* 問題分析:因為頁面上存在相同的停車場資訊(停車場名稱,停車場地址和停車場車位個數相同),那麼匯入到excel中也存在相同資訊,
* 客戶需求是相同的資訊只需保留一項,所以在匯入前需要過濾.
* @author IluckySi
* @since 20150210
*/
public class MainTest {
public static void main(String[] args) {
//抓取資料.
WebSpiderUtil wsu = new WebSpiderUtil();
wsu.setUrl("http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=");
long start = System.currentTimeMillis();
wsu.caputerData();
System.out.println("抓取資料共耗時:" + (System.currentTimeMillis() - start)/1000 + "秒");
//過濾重複資料.
List<DataModel> all = wsu.getDataModelList();
List<DataModel> del = new ArrayList<DataModel>();
List<DataModel> save = new ArrayList<DataModel>();
System.out.println("抓取到的資料共:" + all.size());
for(int i = 0; all != null && i < all.size(); i++) {
boolean flag = false;
DataModel dm = all.get(i);
for(int j = 0; save != null && j < save.size(); j++) {
DataModel dm2 = save.get(j);
if(dm.getParkAddress().equals(dm2.getParkAddress()) &&
dm.getParkName().equals(dm2.getParkName()) &&
dm.getParkCount().equals(dm2.getParkCount())) {
del.add(dm2);
flag = true;
break;
}
}
if(flag == false) {
save.add(dm);
}
}
//將過濾後資料寫入到excel中.
JxlUtil ju = new JxlUtil();
ju.setPath("C:\\Users\\IluckySi\\Desktop\\model.xls");
Map<String, List<List<String>>> listListMap = new HashMap<String, List<List<String>>>();
List<List<String>> listList = new ArrayList<List<String>>();
List<String> list1 = new ArrayList<String>();
list1.add("停車場名稱");
list1.add("地址");
list1.add("泊位數");
list1.add("價格");
listList.add(list1);
for(DataModel dm : save) {
List<String> list = new ArrayList<String>();
list.add(dm.getParkName());
list.add(dm.getParkAddress());
list.add(dm.getParkCount());
list.add(dm.getParkPrice());
listList.add(list);
}
System.out.println("存入到excel的資料共:" + save.size());
listListMap.put("上海停車網", listList);
ju.write(listListMap);
}
}
<pre code_snippet_id="602483" snippet_file_name="blog_20150211_3_6245062" name="code" class="java">/**
控制檯輸出:
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=20
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=40
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=60
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=80
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=100
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=120
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=140
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=160
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=180
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=200
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=220
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=240
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=260
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=280
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=300
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=320
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=340
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=360
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=380
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=400
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=420
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=440
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=460
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=480
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=500
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=520
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=540
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=560
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=580
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=600
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=620
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=640
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=660
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=680
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=700
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=720
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=740
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=760
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=780
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=800
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=820
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=840
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=860
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=880
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=900
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=920
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=940
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=960
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=980
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1000
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1020
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1040
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1060
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1080
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1100
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1120
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1140
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1160
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1180
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1200
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1220
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1240
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1260
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1280
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1300
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1320
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1340
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1360
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1380
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1400
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1420
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1440
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1460
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1480
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1500
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1520
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1540
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1560
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1580
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1600
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1620
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1640
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1660
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1680
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1700
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1720
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1740
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1760
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1780
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1800
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1820
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1840
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1860
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1880
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1900
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1920
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1940
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1960
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=1980
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2000
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2020
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2040
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2060
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2080
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2100
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2120
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2140
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2160
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2180
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2200
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2220
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2240
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2260
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2280
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2300
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2320
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2340
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2360
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2380
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2400
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2420
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2440
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2460
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2480
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2500
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2520
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2540
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2560
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2580
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2600
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2620
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2640
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2660
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2680
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2700
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2720
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2740
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2760
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2780
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2800
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2820
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2840
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2860
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2880
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2900
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2920
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2940
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2960
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=2980
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3000
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3020
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3040
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3060
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3080
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3100
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3120
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3140
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3160
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3180
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3200
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3220
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3240
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3260
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3280
正在抓取的頁面url = http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=3300
抓取資料共耗時:58秒
抓取到的資料共:3318
存入到excel的資料共:2465
成功寫入檔案
*/
8.最後看匯出工具類:
package com.ilucky.util.jxl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
/**
* 注意:此工具類只支援解析excel2003,不支援解析高版本的excel,如果解析高版本excel會報如下錯誤:
* jxl.read.biff.BiffException: Unable to recognize OLE stream
* 解決方案:將高版本excel檔案另存為Excel97-2003工作薄,然後再解析.
* jxl較poi的好處是跨平臺,因為是用純java編寫,poi雖然功能比jxl強大,但是是基於windows系統的.
* @author IluckySi
* @since 20141215
*/
public class JxlUtil {
private String filePath;
public String getPath() {
return filePath;
}
public void setPath(String filePath) {
this.filePath = filePath;
}
/**
* 解析excel檔案.
* @return Map<String, List<List<String>>>
*/
public Map<String, List<List<String>>> parse() {
File file = new File(filePath);
if(!file.exists() || !file.getName().endsWith(".xls")) {
try {
throw new Exception("要解析的路徑有問題: " + filePath);
} catch (Exception e) {
e.printStackTrace();
}
}
Map<String, List<List<String>>> listListMap = new HashMap<String, List<List<String>>>();
Workbook workBook = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
workBook = Workbook.getWorkbook(fis);
Sheet[] sheetArray = workBook.getSheets();
for(int i = 0; sheetArray != null && i < sheetArray.length; i++) {
Sheet sheet = sheetArray[i];
List<List<String>> listList = parseSheet(sheet);
if(listList != null && listList.size() > 0) {
listListMap.put(sheet.getName(), listList);
}
}
} catch (BiffException e) {
System.out.println("解析檔案發生異常: " + e);
} catch (IOException e) {
System.out.println("解析檔案發生異常: " + e);
} finally {
try {
if(workBook != null) {
workBook.close();
workBook = null;
}
if(fis != null) {
fis.close();
fis = null;
}
} catch (Exception e) {
System.out.println("關閉檔案流發生異常: " + e);
}
}
return listListMap;
}
/**
* 解析sheet,需要注意的地方:合併單元格,
* 例:如果A6-A12合併了單元格,那麼解析excel時,解析類庫只認為A6有值.
* @param sheet
*/
private List<List<String>> parseSheet(Sheet sheet) {
List<List<String>> listList = new ArrayList<List<String>>();
int rowCount = sheet.getRows();
for(int i = 1; i < rowCount; i++) {
List<String> list = new ArrayList<String>();
Cell[] cellArray = sheet.getRow(i);
for(int j = 0; cellArray != null && j < cellArray.length; j++) {
list.add(cellArray[j].getContents());
}
listList.add(list);
}
return listList;
}
/**
* 將資料來源寫入到excel中.
* 注意:20150211加的寫入方法.
* @param listListMap
* @return
*/
public boolean write(Map<String, List<List<String>>> listListMap) {
File file = new File(filePath);
boolean result = false;
WritableWorkbook workBook = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
workBook = Workbook.createWorkbook(fos);
int sheetNo = 0;
for(Entry<String, List<List<String>>> entry : listListMap.entrySet()) {
String key = entry.getKey();
List<List<String>> listList = entry.getValue();
WritableSheet sheet = workBook.createSheet(key, sheetNo++);
for(int i = 0; i < listList.size(); i++) {
List<String> list = listList.get(i);
for(int j = 0; j < list.size(); j++) {
Label label = new Label(j, i, list.get(j));
sheet.addCell(label);
}
}
}
workBook.write();
System.out.println("成功寫入檔案");
} catch (Exception e) {
System.out.println("寫入檔案發生異常: " + e);
} finally {
try {
if(workBook != null) {
workBook.close();
}
if(fos != null) {
fos.close();
}
} catch (IOException e) {
System.out.println("關閉檔案流發生異常: " + e);
}
}
return result;
}
}
ok! 大功告成!看成果吧!
注意:由於我水平有限,程式碼寫的比較死!後期會完善此工具類!也希望有網路爬蟲經驗的大神給與指導!萬分感謝!
相關推薦
如何通過jsoup網路爬蟲工具爬取網頁資料,並通過jxl工具匯出到excel
1:閒話少說,直接看需求: 抓取的url:http://www.shparking.cn/index.php/welcome/municipal_parking?key=&per_page=. 參考的資料:http://blog.csdn.net/lmj6235
網路爬蟲之爬取網頁圖片並儲存
爬取網頁圖片並儲存在本地 將網頁上的圖片爬取之後,以圖片原有名字儲存在本地 程式碼: import requests import os url="http://p1.so.qhmsg.com/bdr/_240_/t01dab8b2e73fe661d6
python實戰之網路爬蟲(爬取網頁新聞資訊列表)
關於大資料時代的資料探勘 (1)為什麼要進行資料探勘:有價值的資料並不在本地儲存,而是分佈在廣大的網路世界,我們需要將網路世界中的有價值資料探勘出來供自己使用 (2)非結構化資料:網路中的資料大多是非結構化資料,如網頁中的資料都沒有固定的格式 (3)非結構化資料的挖掘--ETL:即三個步
Python——網路爬蟲(爬取網頁圖片)
最近在學習 Python, 然後就試著寫了一個簡單的Python小程式,爬取一個網頁的圖片,不得不說 Python 真的強大,以下是爬取 NEFU Online Judge 網站的程式碼。 吐槽:其實
你以為Python爬蟲只能爬取網頁資料嗎?APP也是可以的呢!
摘要 大多數APP裡面返回的是json格式資料,或者一堆加密過的資料 。這裡以超級課程表APP為例,抓取超級課程表裡使用者發的話題。 1 抓取APP資料包 方法詳細可以參考這篇博文:http://my.oschina.net/jhao104/blog/605963 得到超級課程表
Python爬取網頁資料並匯入表格
import requests import time import random import socket import http.client from bs4 import BeautifulSoup import csv def getContent(url
Python爬蟲 BeautifulSoup抓取網頁資料 並儲存到資料庫MySQL
最近剛學習Python,做了個簡單的爬蟲,作為一個簡單的demo希望幫助和我一樣的初學者 程式碼使用python2.7做的爬蟲 抓取51job上面的職位名,公司名,薪資,釋出時間等等 直接上程式碼,程式碼中註釋還算比較清楚 ,沒有安裝mysql需要遮蔽掉相關程式碼:#!/u
網路爬蟲:爬取動態網頁
import requests from bs4 import BeautifulSoup res = requests.get('http://news.sina.com.cn/c/nd/2017-06-12/doc-ifyfzhac1650783.shtml') res.encoding = '
Python網路爬蟲之爬取淘寶網頁頁面 MOOC可以執行的程式碼
可以實現功能的全部程式碼: import requests import re def getHTMLText(url): try: r = requests.get(url, timeout = 30) r.raise_for_statu
python實戰之網路爬蟲(爬取新聞內文資訊)
(1)前期準備:開啟谷歌瀏覽器,進入新浪新聞網國內新聞頁面,點選進入其中一條新聞,開啟開發者工具介面。獲取當前網頁資料,然後使用BeautifulSoup進行剖析,程式碼: import requests from bs4 import BeautifulSoup res = requests.
網路爬蟲定時爬取的相關方法
關於python的定時爬取相關方法:雖然time模組的time.sleep()方法使程式休眠來達到定時任務的目的,這樣也可以,但是總覺得不是那麼的專業,所以就使用如下python的定時任務模組APScheduler: 首先安裝相關pip:pip install apscheduler
爬蟲——爬取網頁資料存入表格
最近由於個人需要,從相關書籍以及網上資料進行爬蟲自學,目標網址為http://mzj.beijing.gov.cn,對其內容進行整理篩選,存入excel格式。 首先是對錶格的內容進行設定,編碼格式定義為utf-8,新增一個sheet的表格,其中head為表頭的內容,定義之後,利用sheet.wr
JAVA爬蟲爬取網頁資料資料庫中,並且去除重複資料
pom檔案 <!-- 新增Httpclient支援 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId&
python 爬蟲(爬取網頁的img並下載)
from urllib.request import urlopen # 引用第三方庫 import requests #引用requests/用於訪問網站(沒安裝需要安裝) from pyquery import PyQuery as pq #引用PyQuery用於 解析 # def get_url(
java實現簡單的網路爬蟲(爬取電影天堂電影資訊)
在最開始,我們要在網上下載所用到的jar包,應為這只是一個簡單的網路爬蟲所以很多包裡的內容沒有用到。 下面幾個包就可以了。並且要引入這些包。 主類Bigdata.javaimport org.htmlparser.util.ParserException; public
python初級實戰系列教程《一、爬蟲之爬取網頁、圖片、音視訊》
python基礎知識可以到廖雪峰大佬的官網學習哦! 廖雪峰官網網址 學完python就開始我們的實戰吧!首先我們就來學習下python爬蟲 學習Python爬蟲,先是介紹一個最容易上手的庫urll
python3爬蟲 -----爬取大學資訊並通過matplotlib與numpy繪製結果-----from最好大學網
爬取大學資訊並 通過matplotlib與numpy繪製多指標柱形圖 就某一因素繪製餅圖並突出其中一個物件(本例中為江西理工大學) 1 # -*- coding:utf-8 -*- 2 # author:zxy 3 # date:2018-12-24 4 # upda
python爬蟲小試例項--爬取網頁圖片並下載
一、python安裝在python的官網下載python版本,需要下載對應版本(在計算機-屬性中檢視自己是32位作業系統還是64位作業系統),我是64位的,就下載64位對應的安裝包了(如下圖:Windows x86-64 executable installer)。官網下載地
Python爬蟲爬取網頁資料並存儲(一)
環境搭建 1.需要事先安裝anaconda(或Python3.7)和pycharm *anaconda可在中科大映象下下載較快 2.安裝中遇到的問題: *anaconda(記得安裝過程中點新增路徑到path裡,沒新增的話手動新增: 計算機右鍵屬性——高階系統設
不會Python爬蟲?教你一個通用爬蟲思路輕鬆爬取網頁資料
前言 其實爬蟲的思路很簡單,但是對於很多初學者而言,看得懂,但是自己寫的時候就不知道怎麼去分析了!說實話還是寫少了,自己不要老是抄程式碼,多動手! 本人對於Python學習建立了一個小小的學習圈子,為各位提供了一個平臺,大家一起來討論學習Python。歡迎各位