WebClient 基本使用,模仿网页请求【L】
来源:互联网 发布:数据库无法写入中文 编辑:程序博客网 时间:2024/05/21 17:32
package com.lxl.htmlutil.test;import java.io.IOException;import org.junit.Test;import com.gargoylesoftware.htmlunit.WebClient;import com.gargoylesoftware.htmlunit.html.DomElement;import com.gargoylesoftware.htmlunit.html.DomNodeList;import com.gargoylesoftware.htmlunit.html.HtmlAnchor;import com.gargoylesoftware.htmlunit.html.HtmlElement;import com.gargoylesoftware.htmlunit.html.HtmlPage;import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;public class HtmlUtilTest {@Testpublic void test() throws IOException {// 创建浏览器,可以选择IE、FF等等WebClient client = new WebClient(); //htmlunit 对css和javascript的支持不好,所以请关闭之client.getOptions().setJavaScriptEnabled(false);client.getOptions().setCssEnabled(false);// 获取某网站页面HtmlPage loginPage = client.getPage("http://175.43.121.77/mblog/login");HtmlElement passwordElmt = loginPage.getElementByName("password");passwordElmt.click();passwordElmt.type("3323356");// 获取某页面元素,可通过id或name,(具体方式很多 --Foxswily)HtmlElement usernameElmt = loginPage.getElementByName("username");// HtmlElement elmt = page.getElementByName("somename");// 此例以文本框为例,先点击,再输入,完全跟实际浏览器行为一致usernameElmt.click();usernameElmt.type("q260996583");// 获取按钮DomNodeList<DomElement> loginBtn = loginPage.getElementsByTagName("input");// HtmlSubmitInput loginBtn = (HtmlSubmitInput) loginPage// .getElementById("LoginAction_login");// HtmlButton loginBtn = (HtmlButton)// page.getElementById("LoginAction_login");HtmlSubmitInput input = null;for(DomElement dom : loginBtn) {if(dom.getAttribute("type").equals("submit")){input = (HtmlSubmitInput) dom;}}// 点击并获得返回结果HtmlPage loginResultPage = input.click();// 结果拿到了,想干啥您随意String loginResultContent = loginResultPage.getWebResponse().getContentAsString();System.out.println(loginResultContent);// 如果登录成功if (null != loginResultContent&& loginResultContent.indexOf("龙马") > 0) {// 获得HTML链接并点击该链接HtmlAnchor showbookBtn = (HtmlAnchor) loginResultPage.getElementById("showbook");HtmlPage showBookPage = showbookBtn.click();System.out.println("\n\n===========================================================");System.out.println(showBookPage.getWebResponse().getContentAsString());}}}
pom.xml
htmlunit 在httpclient项目上增加新功能,htmlunit中包含了httpclient、httpmime等jar包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lxl.test</groupId> <artifactId>SpringSplit</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringSplit Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- https://mvnrepository.com/artifact/net.sourceforge.htmlunit/htmlunit --><dependency> <groupId>net.sourceforge.htmlunit</groupId> <artifactId>htmlunit</artifactId> <version>2.15</version></dependency> </dependencies> <build> <finalName>htmlutil</finalName> </build></project>
阅读全文
0 0
- WebClient 基本使用,模仿网页请求【L】
- HttpURLConnection基本使用【L】
- 使用WebClient
- Http请求之 WebClient
- WebClient发送POST请求
- webclient请求返回乱码
- WebClient发送POST请求
- WebClient获取网页程序
- WebClient获取网页代码
- c# webclient下载网页
- 网页模仿
- C# WebRequest WebClient Post请求
- C# Http请求(WebClient,HttpWebRequest)
- 利用WebClient读取网页源代码
- WebClient.DownLoadData下载网页内容
- React Native使用本地json模仿数据请求
- 使用WebClient实现下载
- WebClient类的使用
- C# yield
- PSPNT3.0打开彩色图输出C、M、Y、K四色图
- 分布式服务框架dubbo原理解析
- maven 启动tomcat 报错Failed to start component [StandardEngine[Tomcat].StandardHost
- 【Linux】epoll 服务器
- WebClient 基本使用,模仿网页请求【L】
- 自定义 input type=file样式
- Android开发工具下载及环境配置
- Lua基础---循环语句
- [LeetCode]147. Insertion Sort List 深入浅出讲解和代码示例
- Servlet如何获取Spring里的数据[L]
- 安卓系统广播大全
- Ubuntu下安装网易云音乐
- JVM内存调优