浏览器模拟工具 - HtmlUnit
最编程
2024-06-23 14:45:56
...
开源中国社区团队直播首秀,以分享为名讲述开源中国社区背后的故事”
随着Web的发展,RIA越来越多,JavaScript和Complex AJAX Libraries给网络爬虫带来了极大的挑战,解析页面的时候需要模拟浏览器执行JavaScript才能获得需要的文本内容。
好在有一个Java开源项目HtmlUnit,它能模拟Firefox、IE、Chrome等浏览器,不但可以用来测试Web应用,还可以用来解析包含JS的页面以提取信息。
下面看看HtmlUnit的效果如何:
首先,建立一个maven工程,引入junit依赖和HtmlUnit依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.14</version>
</dependency>
其次,写一个junit单元测试来使用HtmlUnit提取页面信息:
/**
* 使用HtmlUnit模拟浏览器执行JS来获取网页内容
* @author 杨尚川
*/
public class HtmlUnitTest {
@Test
public void homePage() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_11);
final HtmlPage page = webClient.getPage("http://yangshangchuan.iteye.com");
Assert.assertEquals("杨尚川的博客 - ITeye技术网站", page.getTitleText());
final String pageAsXml = page.asXml();
Assert.assertTrue(pageAsXml.contains("杨尚川,系统架构设计师,系统分析师,2013年度优秀开源项目APDPlat发起人,资深Nutch搜索引擎专家。多年专业的软件研发经验,从事过管理信息系统(MIS)开发、移动智能终端(Win CE、Android、Java ME)开发、搜索引擎(nutch、lucene、solr、elasticsearch)开发、大数据分析处理(Hadoop、Hbase、Pig、Hive)等工作。目前为独立咨询顾问,专注于大数据、搜索引擎等相关技术,为客户提供Nutch、Lucene、Hadoop、Solr、ElasticSearch、HBase、Pig、Hive、Gora等框架的解决方案、技术支持、技术咨询以及培训等服务。"));
final String pageAsText = page.asText();
Assert.assertTrue(pageAsText.contains("[置顶] 国内首套免费的《Nutch相关框架视频教程》(1-20)"));
webClient.closeAllWindows();
}
@Test
public void homePage_Firefox() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
final HtmlPage page = webClient.getPage("http://yangshangchuan.iteye.com");
Assert.assertEquals("杨尚川的博客 - ITeye技术网站", page.getTitleText());
webClient.closeAllWindows();
}
@Test
public void getElements() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
final HtmlPage page = webClient.getPage("http://yangshangchuan.iteye.com");
final HtmlDivision div = page.getHtmlElementById("blog_actions");
//获取子元素
Iterator<DomElement> iter = div.getChildElements().iterator();
while(iter.hasNext()){
System.out.println(iter.next().getTextContent());
}
//获取所有输出链接
for(HtmlAnchor anchor : page.getAnchors()){
System.out.println(anchor.getTextContent()+" : "+anchor.getAttribute("href"));
}
webClient.closeAllWindows();
}
@Test
public void xpath() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://yangshangchuan.iteye.com");
//获取所有博文标题
final List<HtmlAnchor> titles = (List<HtmlAnchor>)page.getByXPath("/html/body/div[2]/div[2]/div/div[16]/div/h3/a");
for(HtmlAnchor title : titles){
System.out.println(title.getTextContent()+" : "+title.getAttribute("href"));
}
//获取博主信息
final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@id='blog_owner_name']").get(0);
System.out.println(div.getTextContent());
webClient.closeAllWindows();
}
@Test
public void submittingForm() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24);
final HtmlPage page = webClient.getPage("http://www.oschina.net");
// Form没有name和id属性
final HtmlForm form = page.getForms().get(0);
final HtmlTextInput textField = form.getInputByName("q");
final HtmlButton button = form.getButtonByName("");
textField.setValueAttribute("APDPlat");
final HtmlPage resultPage = button.click();
final String pageAsText = resultPage.asText();
Assert.assertTrue(pageAsText.contains("找到约"));
Assert.assertTrue(pageAsText.contains("条结果"));
webClient.closeAllWindows();
}
}
最后,我们运行单元测试, 全部通过测试!
上一篇: 在 HtmlUnit 中屏蔽日志
推荐阅读
-
浏览器F12(开发者工具DevTools)的屏蔽调用与绕过屏蔽
-
3款必备手机浏览器,内置丰富工具,而且全部免费!你尝试过第三款——夸克浏览器了吗?
-
探究Java单元测试中的模拟工具Mockito:原理详解
-
轻松使用雷电模拟器和Burpsuite抓取HTTPS数据:只需导入证书,就像在浏览器中一样简单
-
如何在Win10上使用Charles进行下载、安装、证书设置以及与雷电模拟器或浏览器的抓包测试
-
试试这个简单好用、适用于Android和iOS的弱网模拟工具吧!
-
简易百度翻译爬虫实践 - 编码 UTF-8 - 模拟浏览器访问网页,创建文件夹并存储文本文件,命名文件为单词形式
-
如何在uniapp中实现在真机、手机模拟器与微信开发者工具里的运行与调试操作指南
-
玩转JVM:模拟内存爆满情况、用MAT工具深度剖析、阅读并理解GC日志、细说JVM常用设置技巧
-
多功能Dymola模拟工具平台