因其项目较特殊,无法使用httpclient来获取数据,但需求必须实现,所以采用了两种方法来获取数据,前提是有固定的账号和密码能够登陆网站
一、使用selenium工具
- 先设置maven依赖
因其项目只支持IE8,在进行多次测试后,版本选择2.13
使用前需下载对应的驱动 driver.exe
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.50.0</version>
</dependency>
public void seleniumOpWx(String xdts,String sgrq,String keyWords) throws InterruptedException, ParseException{
Date date = DateUtils.parseDate(sgrq);
//登录
System.setProperty("webdriver.ie.driver", "D:\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://x.x.x.x:xxxx/login.jsp");
WebDriverWait waitLogin = new WebDriverWait(driver, 30);
//显示等待-当此元素可被点击时再执行接下来的代码,以此判断是否登录成功
waitLogin.until(ExpectedConditions.elementToBeClickable(By.id("username")));
//操作登录
WebElement j_username = driver.findElement(By.id("username"));
WebElement j_password = driver.findElement(By.id("password"));
WebElement secure = driver.findElement(By.id("secure"));
username.sendKeys("admin");
password.sendKeys("admin");
WebElement submit = driver.findElement(By.id("pub"));
submit.click();
//登录后跳转到指定页面操作查询获取数据,其页面未分页
driver.get("http://x.x.x.x:xxxx/h.jsp");
String[] dateArr = sgrq.split("-");
String sgrqDate = dateArr[0]+dateArr[1]+dateArr[2];
String[] xdtArr = xdts.split(",");
for (String xdt : xdtArr) {
WebElement info = driver.findElement(By.xpath("//td[@title='"+xdt+"']"));
//显示等待页面加载完成
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.elementToBeClickable(info));
info.click();
try {
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
String dy_str = driver.findElement(By.xpath("//*[@id='gdbTitleText']/tbody/tr[1]/td[2]")).getText();
if (dy_str.contains("没有")) {
continue;
}
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("style42")));
//获取标题
String dynum_str = driver.findElement(By.xpath("//*[@id='gdbTitleText']/tbody/tr[1]/td[2]")).getText();
if (!dynum_str.contains("没有")) {
List<WebElement> tables = driver.findElements(By.id("gdbId"));
if (tables.size()>0) {
for (WebElement table : tables) {
List<WebElement> rows = table.findElements(By.className("style42"));
for(int i=0 ; i<rows.size();i++) {
List<WebElement> coList = rows.get(i).findElements(By.tagName("td"));
if (coList.get(5).getText().contains((StringUtils.isEmpty(keyWords)?"hh":keyWords))) {
//此处将获取的数据进行存储
}
}
}
}
}
} catch (Exception e) {
continue;
}
}
driver.quit();
}
二、使用htmlunit
测试挑选出适合的版本,配置依赖
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.13</version>
</dependency>
public void consInport(String xdts,String dateStr,String keyWords) throws FailingHttpStatusCodeException, IOException, ParseException {
Date planDate = DateUtils.parseDate(dateStr);
String[] xdtArr = xdts.split(",");
StringBuilder dateBuilder = new StringBuilder();
String[] dateArr = dateStr.split("-");
dateBuilder.append(dateArr[0]).append("年").append(dateArr[1]).append("月").append(dateArr[2]).append("日");
List<RepairPlanReview> repairPlanReviews = new ArrayList<RepairPlanReview>();
WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_8);
webClient.getOptions().setJavaScriptEnabled(true);//支持js
webClient.getOptions().setCssEnabled(false);//支持CSS,一般CSS不需要
webClient.setAjaxController(new NicelyResynchronizingAjaxController());//配置使用ajax,没有这一项调用网页接口会失败
webClient.getOptions().setThrowExceptionOnScriptError(false);
try {
HtmlPage htmlPage = webClient.getPage("http://路径");//进入登录页面
HtmlInput usernameInput = htmlPage.getHtmlElementById("username");//通过getHtmlElementById获取输入框
HtmlInput pswInput = htmlPage.getHtmlElementById("password");
// 往输入框 “填值”
usernameInput.setValueAttribute("admin");
pswInput.setValueAttribute("admin");
// 获取登陆按钮
HtmlInput btn = htmlPage.getHtmlElementById("btnenter");
btn.click();//按下登陆按钮
//模拟遍历点击并查询
for (String xdt : xdtArr) {
HtmlPage htmlPage2 = webClient.getPage("http://路径");
HtmlInput date = htmlPage2.getHtmlElementById("query_plandate");
date.setValueAttribute(dateBuilder.toString());
HtmlSelect xdtSelect = htmlPage2.getElementByName("query_attempername");
HtmlInput query = htmlPage2.getHtmlElementById("btnquery");//查询按钮
HtmlOption xdtOption =xdtSelect.getOptionByValue(xdt);
if (xdtOption != null) {
htmlPage2 = xdtOption.click();
htmlPage2 = query.click();
//获取数据表格
HtmlTable table = htmlPage2.getHtmlElementById("listTable");
if (table!= null) {
//获取每行数据
List<HtmlTableRow> rows = table.getRows();
if (rows.size()>=2) {
//这里存储数据
}
}
}
}
} catch (FailingHttpStatusCodeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}