- 準備工作
- 啟動網頁解析器
- 項目配置
- seimi.properties
- SeimiAgentDemo.java
- 分析原網頁代碼
- Boot.java
- 同系列文章
準備工作
新手的話推薦使用seimiagent+seimicrawler的爬取方式,非常容易上手,輕松爬取動態網頁,目測初步上手10分鐘以內吧。
項目地址
https://github.com/a252937166/seimicrawler
網頁解析工具地址
https://github.com/a252937166/seimiagent
啟動網頁解析器
根據系統選擇所需文件
下載好seimiagent,根據自己的操作系統,如果是windows,就用seimiagent.exe,如果是linux,就選擇seimiagent,mac版本暫時還沒有,我的話一般會把seimiagent放在自己的linux服務器上。
指定端口號啟動工具
以linux為例,進入文件所在目錄,指定8000端口,./seimiagent -p 8000,即可啟動。
圖(1)
項目配置
seimi.properties
redis
.host=
127.0.0.1
redis
.port=
6379
redis
.password=
database
.driverClassName=
com.mysql.jdbc.Driver
database
.url=
database
.username=
database
.password=
seimiAgentHost=
127.0.0.1
seimiAgentPort=
8000
找到此配置文件,修改seimiAgentHost和seimiAgentPort為自己的地址信息。
SeimiAgentDemo.java
package com.ouyang.crawlers;
import cn.wanghaomiao.seimi.annotation.Crawler;
import cn.wanghaomiao.seimi.def.BaseSeimiCrawler;
import cn.wanghaomiao.seimi.struct.Request;
import cn.wanghaomiao.seimi.struct.Response;
import cn.wanghaomiao.xpath.model.JXDocument;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
/*** 這個例子演示如何使用SeimiAgent進行復雜動態頁面信息抓取* @author 汪浩淼 et.tw@163.com* @since 2016/4/14.*/
@Crawler(name =
"seimiagent")
public class SeimiAgentDemo extends BaseSeimiCrawler{/*** 在resource/config/seimi.properties中配置方便更換,當然也可以自行根據情況使用自己的統一配置中心等服務*/@Value(
"${seimiAgentHost}")
private String seimiAgentHost;
@Value(
"${seimiAgentPort}")
private int seimiAgentPort;
@Overridepublic String[]
startUrls() {
return new String[]{
"https://www.baidu.com"};}
@Overridepublic String
seimiAgentHost() {
return this.seimiAgentHost;}
@Overridepublic int seimiAgentPort() {
return this.seimiAgentPort;}
@Overridepublic void start(Response response) {Request seimiAgentReq = Request.build(
"http://manhua.fzdm.com/2/889/",
"getHtml").useSeimiAgent()
.setSeimiAgentRenderTime(
5000);push(seimiAgentReq);}
/*** 打印網頁信息* @param response*/public void getHtml(Response response){
try {System.out.println(response.getContent());}
catch (Exception e) {e.printStackTrace();}}
}
找到這個demo文件,注意.useSeimiAgent(),這就是表示需要使用seimiagent來解析動態網頁了,此外還可以設置cookie,param,meta這些參數。
我們使用getHtml()這個回調函數來打印網頁信息,對比一下看看,有沒有順利解析成功。
分析原網頁代碼
我們使用chrome瀏覽器的network查看網頁原始代碼。
圖(2)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="utf-8">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="utf-8" />
<meta content="all" name="robots" />
<title>海賊王889話 風之動漫
</title>
<meta name="keywords" content="海賊王889話 " />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta name="applicable-device" content="pc,mobile" />
<meta name="HandheldFriendly" content="true" />
<meta property="og:title" content="海賊王889話"/>
<meta property="og:type" content="book"/>
<meta property="og:url" id="readurl" content="http://manhua.fzdm.com/2/889/" /><link rel="stylesheet" href="//static.fzdm.com/pure/pure-min.css">
<link rel="stylesheet" href="//static.fzdm.com/pure/grids-responsive-min.css">
<link rel="stylesheet" href="//static.fzdm.com/pure/fzdm.css">
<link rel="icon" href="//static.fzdm.com/favicon.ico" mce_href="//static.fzdm.com/favicon.ico" type="image/x-icon">
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link rel="apple-touch-icon" href="//static.fzdm.com/apple-touch-icon-144x144.png" /><style>
.logo {top: -2px;height: 70px;overflow: hidden;}.logo img{height:77px}
#header {height: 70px;
}
#header ul {top: 8px;}.pure-menu.pure-menu-open, .pure-menu.pure-menu-horizontal li .pure-menu-children {text-align: left;height: 70px;background: none;}</style><script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?cb51090e9c10cda176f81a7fa92c3dfc";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script></head>
<body>
<script src="//static.fzdm.com/jquery-1.9.1.min.js?v=1"></script>
<script src="//static.fzdm.com/fzdm.js?v=1"></script>
<script src="//static.fzdm.com/u.js"></script><script src="//dup.baidustatic.com/js/dm.js"></script>
<div id="header">
<div class="pure-g">
<div class="pure-menu pure-menu-open pure-menu-horizontal">
<div class="logo">
<a href="//www.fzdm.com"><img src="//static.fzdm.com/css/logo.png" alt="風之動漫" /></a>
</div>
<ul>
<li><a href="//www.fzdm.com/"> 首頁
</a></li>
<li><a href="//news.fzdm.com/">動漫新聞
</a></li>
<li><a href="//manhua.fzdm.com/">在線漫畫
</a></li>
<li><a href="//flash.fzdm.com/">動漫flash
</a></li></ul></div></div></div></div><center>
</center><br><br>
<div id="weizhi">位置:
<a href="//www.fzdm.com">首頁
</a> -
<a href="../../">在線漫畫
</a> -
<a href="../">海賊王
</a> - 海賊王889話
<h4 style="float:right;margin-right: 100px;"><a href="#comments">海賊王889話討論區
</a></h4></div>
<div id="mh">
<h1>海賊王889話
</h1><div id="mhimg0"><h2><a href="//manhua.fzdm.com/2/889/">《無法觀看》請點擊此處~
</a></h2></div><center><div id="share">
<div class="bdsharebuttonbox"><a href="#" class="bds_more" data-cmd="more">分享
<strong>海賊王889話漫畫
</strong>到:
</a><a href="#" class="bds_qzone" data-cmd="qzone" title="分享到QQ空間">QQ空間
</a><a href="#" class="bds_weixin" data-cmd="weixin" title="分享到微信">微信
</a><a href="#" class="bds_sqq" data-cmd="sqq" title="分享到QQ好友">QQ好友
</a><a href="#" class="bds_tsina" data-cmd="tsina" title="分享到新浪微博">微博
</a><a href="#" class="bds_tqq" data-cmd="tqq" title="分享到騰訊微博">騰訊
</a><a href="#" class="bds_renren" data-cmd="renren" title="分享到人人網">人人網
</a><a href="#" class="bds_fbook" data-cmd="fbook" title="分享到Facebook">Facebook
</a><a href="#" class="bds_baidu" data-cmd="baidu" title="分享到百度搜藏">百度搜藏
</a><a href="#" class="bds_bdhome" data-cmd="bdhome" title="分享到百度新首頁">百度首頁
</a><a class="bds_count" data-cmd="count"></a></div>
</div><div id="ad">
<script src='//m.xmshqh.com/fz2.js'></script>
</div></center>
<div class="navigation">
<a href="index_0.html" id="mhona">第1頁
</a><a href="index_1.html">2
</a><a href="index_2.html">3
</a><a href="index_3.html">4
</a><a href="index_4.html">5
</a><a href="index_5.html">6
</a><a href="index_6.html">7
</a><a href="index_7.html">8
</a><a href="index_8.html">9
</a><a href="index_9.html">10
</a><a href="index_10.html">11
</a><a href="index_11.html">12
</a><a href="index_12.html">13
</a><a href="index_13.html">14
</a><a href="index_14.html">15
</a><a href="index_15.html">16
</a><a href="index_16.html">17
</a><a href='index_1.html' id="mhona">下一頁
</a></div><br />
<br />
<script type="text/javascript">document.write('<a style="display:none!important" id="tanx-a-mm_10028503_120355_28042038"></a>');tanx_s = document.createElement("script");tanx_s.type = "text/javascript";tanx_s.charset = "gbk";tanx_s.id = "tanx-s-mm_10028503_120355_28042038";tanx_s.async = true;tanx_s.src = "//p.tanx.com/ex?i=mm_10028503_120355_28042038";tanx_h = document.getElementsByTagName("head")[0];if(tanx_h)tanx_h.insertBefore(tanx_s,tanx_h.firstChild);
</script><script type="text/javascript">document.write('<a style="display:none!important" id="tanx-a-mm_10028503_120355_28058018"></a>');tanx_s = document.createElement("script");tanx_s.type = "text/javascript";tanx_s.charset = "gbk";tanx_s.id = "tanx-s-mm_10028503_120355_28058018";tanx_s.async = true;tanx_s.src = "//p.tanx.com/ex?i=mm_10028503_120355_28058018";tanx_h = document.getElementsByTagName("head")[0];if(tanx_h)tanx_h.insertBefore(tanx_s,tanx_h.firstChild);
</script>
<script type="text/javascript">document.write('<a style="display:none!important" id="tanx-a-mm_10028503_120355_28066012"></a>');tanx_s = document.createElement("script");tanx_s.type = "text/javascript";tanx_s.charset = "gbk";tanx_s.id = "tanx-s-mm_10028503_120355_28066012";tanx_s.async = true;tanx_s.src = "//p.tanx.com/ex?i=mm_10028503_120355_28066012";tanx_h = document.getElementsByTagName("head")[0];if(tanx_h)tanx_h.insertBefore(tanx_s,tanx_h.firstChild);
</script>
<br /><br><br>
<script charset="gbk" src="//p.tanx.com/ex?i=mm_10028503_120355_41360495"></script><br /><br />
<br /><br /><div id="weizhi">熱門漫畫導航:
<a href='//manhua.fzdm.com/91/' target=_blank>美食的俘虜漫畫
</a> -
<a href='//manhua.fzdm.com/7/ 'target=_blank>死神漫畫
</a> -
<a href='//manhua.fzdm.com/39/' target=_blank>進擊的巨人漫畫
</a> -
<a href='//manhua.fzdm.com/35/' target=_blank>家庭教師漫畫
</a> -
<a href="//manhua.fzdm.com/27/" target=_blank>妖精的尾巴漫畫
</a> -
<a href="//manhua.fzdm.com/1/" target=_blank>火影忍者漫畫
</a> -
<a href='//manhua.fzdm.com/53/' target=_blank>黑子的籃球漫畫
</a> -
<a href='//manhua.fzdm.com/45/' target=_blank>惡魔奶爸漫畫
</a> -
<a href='//manhua.fzdm.com/51/' target=_blank>史上最強弟子兼一漫畫
</a> -
<a href='//manhua.fzdm.com/74/' target=_blank>王者天下漫畫
</a> -
<a href='//manhua.fzdm.com/56/' target=_blank>七原罪漫畫
</a> -
<a href='//manhua.fzdm.com/141/' target=_blank>暗殺教室漫畫
</a></div>
<div id="mhimg1"></div>
<script type="text/javascript">var mhurl = "2017/12/22064917941533.jpg";
var mhss = getCookie("picHost");
if (mhss == "") {mhss = "p1.xiaoshidi.net";
}
if (mhurl.indexOf("2015") != -1 || mhurl.indexOf("2016") != -1|| mhurl.indexOf("2017") != -1 || mhurl.indexOf("2018") != -1){}else{mhss = mhss.replace(/p1/,"p0");
};var mhpicurl = mhss+"/"+mhurl;
if (mhurl.indexOf("http") != -1){mhpicurl = mhurl;
};
function nofind(){var img=event.srcElement;img.src="http://p1.xiaoshidi.net/"+mhurl;
var exp = new Date();
exp.setTime(exp.getTime() - 1);
document.cookie = "picHost=0;path=/;domain=fzdm.com;expires="+exp.toGMTString();
img.onerror=null;
};
$("#mhimg0").html('<a href="index_1.html"><img src="http://'+mhpicurl+'" id="mhpic" alt="海賊王889話" onerror="nofind();" /></a>');var mhurl1 = "2017/12/22064917942026.jpg";
mhpicurl = mhss+"/"+mhurl1;
$("#mhimg1").html('<img src="http://'+mhpicurl+'" width="0" height="0" id="mhpic1" />');</script><br />
<br />
<br />
<script>
if (document.location.protocol == "http:"){
window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"海賊王889話 風之動漫","bdUrl":"//manhua.fzdm.com/2/889/","bdDesc":"海賊王889話","bdMini":"2","bdMiniList":false,"bdSign":"","bdPic":"","bdStyle":"0","bdSize":"16"},"share":{"bdSize":16}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
};
</script>
</div><div class="clear"></div>
<div id="footer">
<div id="hd">
<div class="bg"></div>
<br><a href="//www.fzdm.com/about">關于我們
</a> |
<a href="//www.fzdm.com/lianxi">聯系我們
</a> |
<a href="//www.fzdm.com/map">網站地圖
</a><br />
Copyright ⓒ 2014-2015 風之動漫 版本beta 0.3
<br />
</div>
</div><div style="display:none;" ><script src="//static.fzdm.com/stat.js"></script></div></body>
</html>
請注意這段代碼
<div id="mhimg0"><h2><a href="//manhua.fzdm.com/2/889/">《無法觀看》請點擊此處~
</a></h2></div>
如果直接爬取原網頁,肯定沒法獲取圖片的,那么圖片從哪來呢?
var mhurl =
"2017/12/22064917941533.jpg";
var mhss = getCookie(
"picHost");
if (mhss ==
"") {mhss =
"p1.xiaoshidi.net";
}
if (mhurl.indexOf(
"2015") != -
1 || mhurl.indexOf(
"2016") != -
1|| mhurl.indexOf(
"2017") != -
1 || mhurl.indexOf(
"2018") != -
1){}
else{mhss = mhss.replace(
/p1/,
"p0");
};
var mhpicurl = mhss+
"/"+mhurl;
if (mhurl.indexOf(
"http") != -
1){mhpicurl = mhurl;
};
function nofind(){var img=event.srcElement;img.src=
"http://p1.xiaoshidi.net/"+mhurl;
var exp =
new Date();
exp.setTime(exp.getTime() -
1);
document.cookie =
"picHost=0;path=/;domain=fzdm.com;expires="+exp.toGMTString();
img.onerror=
null;
};
$(
"#mhimg0").html(
'<a href="index_1.html"><img src="http://'+mhpicurl+
'" id="mhpic" alt="海賊王889話" onerror="nofind();" /></a>');
這一段js代碼在網頁加載完后自動運行,修改了<div id="mhimg0"></div>的內容,才有了圖片。
js沒有多余請求,只是修改了網頁內容,這種情況如果想直接java解析,只能用正則,而且如果js代碼稍有變化,正則解析就不行了。所以針對這種網頁,直接用SeimiAgent把js渲染之后的網頁返回給我們是最好的處理方式。
Boot.java
package com.ouyang.main;
import cn.wanghaomiao.seimi.core.Seimi;
/*** @author 汪浩淼 [et.tw@163.com]* @since 2015/10/21.*/
public class Boot {public static void main(String[] args){Seimi s =
new Seimi();s.goRun(
"seimiagent");}
}
goRun("seimiagent");填寫對用爬蟲名就行了。
啟動main函數:
圖(3)
seimiagent的解析信息,
windows版本是后臺運行,沒有解析信息。
控制臺信息:
圖(4)
<div id="mhimg0"><a href="index_1.html"><img src="http://p1.xiaoshidi.net/2017/12/22064917941533.jpg" id="mhpic" alt="海賊王889話" onerror="nofind();"></a></div>
這段代碼,很明顯表示,網頁信息已經是成功解析后的了。
總的來說這套爬蟲框架還是很簡單的,想要深入了解框架的同學,可以看看下一篇關于實戰的文章。
同系列文章
java爬蟲系列(一)——爬蟲入門
java爬蟲系列(三)——漫畫網站爬取實戰
java爬蟲系列(四)——動態網頁爬蟲升級版
java爬蟲系列(五)——今日頭條文章爬蟲實戰
總結
以上是生活随笔為你收集整理的java爬虫系列(二)——爬取动态网页的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。