网页信息抓取进阶 Jsoup的不足之处

来源:互联网 发布:而知也无涯成语 编辑:程序博客网 时间:2024/05/17 09:41

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/23866427

今天又遇到一个网页数据抓取的任务,给大家分享下。

说道网页信息抓取,相信Jsoup基本是首选的工具,完全的类JQuery操作,让人感觉很舒服。但是,今天我们就要说一说Jsoup的不足。

1、首先我们新建一个页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>main.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><style type="text/css">a {line-height: 30px;margin: 20px;}</style><!--<link rel="stylesheet" type="text/css" href="./styles.css">--><script type="text/javascript">var datas = [ {href : "http://news.qq.com/a/20140416/017800.htm",title : "高校一<a target=_blank title="保安" href="http://www.cfanz.cn/index.php?c=search&key=%E4%BF%9D%E5%AE%89" target="_blank">保安</a><a target=_blank title="长相" href="http://www.cfanz.cn/index.php?c=search&key=%E9%95%BF%E7%9B%B8" target="_blank">长相</a><a target=_blank title="酷似" href="http://www.cfanz.cn/index.php?c=search&key=%E9%85%B7%E4%BC%BC" target="_blank">酷似</a>作家莫言"}, {href : "http://news.qq.com/a/20140416/015167.htm",title : "男子单臂<a target=_blank title="托举" href="http://www.cfanz.cn/index.php?c=search&key=%E6%89%98%E4%B8%BE" target="_blank">托举</a><a target=_blank title="悬空" href="http://www.cfanz.cn/index.php?c=search&key=%E6%82%AC%E7%A9%BA" target="_blank">悬空</a>女半小时"}, {href : "http://news.qq.com/a/20140416/013808.htm",title : "女子上门讨房租遭<a target=_blank title="强奸" href="http://www.cfanz.cn/index.php?c=search&key=%E5%BC%BA%E5%A5%B8" target="_blank">强奸</a>拍裸照"}, {href : "http://news.qq.com/a/20140416/016805.htm",title : "<a target=_blank title="澳洲" href="http://www.cfanz.cn/index.php?c=search&key=%E6%BE%B3%E6%B4%B2" target="_blank">澳洲</a><a target=_blank title="骆驼" href="http://www.cfanz.cn/index.php?c=search&key=%E9%AA%86%E9%A9%BC" target="_blank">骆驼</a>爱喝冰镇啤酒<a target=_blank title="解暑" href="http://www.cfanz.cn/index.php?c=search&key=%E8%A7%A3%E6%9A%91" target="_blank">解暑</a>"} ];window.onload = function() {var infos = document.getElementById("infos");for( var i = 0 ; i < datas.length ; i++){var a = document.createElement("a");a.href = datas[i].href ;a.innerText = datas[i].title;infos.appendChild(a);infos.appendChild(document.createElement("br"))}}</script></head><body>Hello Main HttpUnit!<br><div id="infos"style="width: 60%; border: 1px solid green; border-radius: 10px; margin: 0 auto;"></div></body></html>
页面上观察是这样显示的:

httpunit

我们审查元素:

jsoup

如果你看到这样的页面,你会觉得拿Jsoup来抓取,简直就是呵呵,小菜一叠,于是我们写了这样的代码:

 @Testpublic void testUserJsoup() {try {Document doc = Jsoup.connect("http://localhost:8080/strurts2fileupload/main.html").timeout(5000).get();Elements links = doc.body().getElementsByTag("a");for (Element link : links) {System.out.println(link.text() + " " + link.attr("href"));}} catch (IOException e) {e.printStackTrace();}}
你会觉得就这几行代码,轻轻松松搞定,快快乐乐下班。于是运行发现,其实什么的抓取不到。

于是我们再回到页面,打开页面源代码,也就是上面的HTML代码,你恍然大悟,我靠,body里面根本没有数据,难怪抓不到。这就是Jsoup的不足,如果Jsoup去抓取的页面的数据,全都是页面加载完成后,ajax获取形成的,是抓取不到的。

下面给大家推荐另一个开源项目:HttpUnit,看名字是用于测试的,但是用来抓取数据也不错

我们开始编写类似Jsoup的代码:

 @Testpublic void testUserHttpUnit() throws FailingHttpStatusCodeException,MalformedURLException, IOException {/** HtmlUnit请求web页面 */WebClient wc = new WebClient(BrowserVersion.CHROME);wc.getOptions().setUseInsecureSSL(true);wc.getOptions().setJavaScriptEnabled(true); // 启用JS解释器,默认为truewc.getOptions().setCssEnabled(false); // 禁用css支持wc.getOptions().setThrowExceptionOnScriptError(false); // js运行错误时,是否抛出异常wc.getOptions().setTimeout(100000); // 设置连接超时时间 ,这里是10S。如果为0,则无限期等待wc.getOptions().setDoNotTrackEnabled(false);HtmlPage page = wc.getPage("http://localhost:8080/strurts2fileupload/main.html");DomNodeList<DomElement> links = page.getElementsByTagName("a");for (DomElement link : links) {System.out.println(link.asText() + " " + link.getAttribute("href"));}}
再看一下运行结果:

jsoup

完美解决,HttpUnit其实就相当于一个没有UI的浏览器,它可以让页面上的js执行完成后,再抓取信息,具体的介绍,google一下就行。主要给大家介绍一种方案!


如果你觉得这篇文章对你有用,就顶一个~

0 0
原创粉丝点击