【HtmlUnit】网页爬虫进阶篇_html/css_WEB-ITnose
之前,亦枫写过一篇关于使用 Jsoup 抓取网页内容的文章:
【Jsoup】HTML解析器,轻松获取网页内容
Jsoup提供的api非常便捷,完全的类似JQuery操作,轻松抓取网页数据。但像Jsoup这样普通的爬虫工具不足的地方就是无法处理js生成的内容。
做过Html开发的人都知道,现在很多网站都在大量使用ajax和JavaScript来获取并处理数据,普通的爬虫工具已经无法处理js中的内容。
举例说明,我们在本地新建一个测试网页文件text.html,源码如下:
<!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://www.jianshu.com/p/8d8edf25850d", title : "推荐一款编程字体,让代码看着更美"}, { href : "http://www.jianshu.com/p/153d9f31288d", title : "Android 利用Camera实现中轴3D卡牌翻转效果"}, { href : "http://www.jianshu.com/p/d6fb0c9c9c26", title : "【Eclipse】挖掘专属最有用的快捷键组合"}, { href : "http://www.jianshu.com/p/72d69b49d135", title : "【IIS】Windows下利用IIS建立网站并实现局域网共享"} ];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> <div class="text" style=" text-align:center;">HtmlUnit 测试网页内容!</div> <br> <div id="infos" style="width: 60%; border: 1px solid green; border-radius: 10px; margin: 0 auto;"> </div> </body></html>
通过IIS发布本地网站(参考亦枫之前写的文章:【IIS】Windows下利用IIS建立网站并实现局域网共享),在浏览器中显示的网页效果如下:
网页展示效果.jpg
虽然通过网页审查元素可以看到body中含有网页展示中的文本内容:
网页审查元素.jpg
但是,通过Jsoup工具根本无法获取!在网页源代码中可以看出,我们需要抓取的内容是在页面显示之后通过ajax和JavaScript加载得到的。
那么怎么办呢?使用本文中推荐的开源工具 —— HtmlUnit,一款能够模拟浏览器的抓包神器!
在官网下载对应jar包,添加到项目工程的lib中,简单的测试代码如下:
import java.io.IOException;import java.net.MalformedURLException;import java.text.ParseException;import com.gargoylesoftware.htmlunit.BrowserVersion;import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;import com.gargoylesoftware.htmlunit.WebClient;import com.gargoylesoftware.htmlunit.html.DomElement;import com.gargoylesoftware.htmlunit.html.DomNodeList;import com.gargoylesoftware.htmlunit.html.HtmlPage;/** * @author 亦枫 * @created_time 2016年1月12日 * @file_user_todo Java测试类 * @blog http://www.jianshu.com/users/1c40186e3248/latest_articles */public class JavaTest { /** * 入口函数 * @param args * @throws ParseException */ public static void main(String[] args) throws ParseException { try { WebClient webClient = new WebClient(BrowserVersion.CHROME); HtmlPage htmlPage = (HtmlPage) webClient.getPage("http://localhost/test.html"); DomNodeList domNodeList = htmlPage.getElementsByTagName("a"); for (int i = 0; i < domNodeList.size(); i++) { DomElement domElement = (DomElement) domNodeList.get(i); System.out.println(domElement.asText()); } webClient.close(); } catch (FailingHttpStatusCodeException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
运行之后,在控制台打印的结果:
HtmlUnit测试结果.jpg
可以看出,HtmlUnit能够抓取到AJAX和JavaScript加载的内容。
有关HtmlUnit的介绍在官网上写的非常详细,以下内容是亦枫翻译的一部分内容,供大家参考:
HtmlUnit是一款基于Java的没有图形界面的浏览器程序。它能够调用HTML文档并且提供API让开发人员像是在一个正常的浏览器上操作一样,获取网页内容,填充表单,点击超链接等等。
它能够非常好的支持JavaScript并且仍在不断改进提升,同时能够解析非常复杂的AJAX库,在不同的配置下模拟Chrome、Firefox和IE浏览器。
HtmlUnit通常用于测试目的和检索网站信息。
HtmlUnit提供了很多测试网络请求和抓取网页内容的功能,大家可以去官网或者其他网站学习使用。
欢迎关注亦枫微信公众号:技术鸟,一起学习,共同进步!
技术鸟_微信二维码.gif

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.
