java webclient_java使用WebClient获取不到重定向的网页信息
就是框框里输入好之后点击下面的按钮
然后它会重定向到下面的网站
可我的代码获取不到重定向的信息
show u codepackage com.cargopm.samoye.service.impl;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class Test3 {
public static void main(String[] args) throws IOException {
String name1="王也";
String name2="诸葛青";
Integer xing1=0;
Integer xing2=0;
Integer sex1=0;
Integer sex2=0;
// 得到浏览器对象,直接New一个就能得到,现在就好比说你得到了一个浏览器了
WebClient webclient = new WebClient();
// 这里是配置一下不加载css和javaScript,配置起来很简单,是不是
webclient.getOptions().setCssEnabled(false);
webclient.getOptions().setJavaScriptEnabled(false);
// 做的第一件事,去拿到这个网页,只需要调用getPage这个方法即可
HtmlPage htmlpage = webclient.getPage("https://www.upsfreight.com/ltl/apps/RateEstimate");
// 根据名字得到一个表单,查看上面这个网页的源代码可以发现表单的名字叫“f”
//final HtmlForm form = htmlpage.getFormByName("f");
//得到网页上第二个表单
final HtmlForm form = htmlpage.getForms().get(1);
HtmlTextInput aaaa= htmlpage.getHtmlElementById("startDate_dtm");
Document document2 = Jsoup.parse(htmlpage.asXml());
Element e1111 = document2.getElementById("startDate_dtm");
//获取网页的配对按钮 (根据class)
HtmlElement button = htmlpage.getHtmlElementById("btnSaveRequest");
// 得到搜索框
HtmlSelect txtUName1 = htmlpage.getHtmlElementById("origCountryCode");
txtUName1.setSelectedIndex(0);
HtmlTextInput txtUName = htmlpage.getHtmlElementById("origZipCode");
txtUName.setValueAttribute("90221");
HtmlSelect txtUName2 = htmlpage.getHtmlElementById("destCountryCode");
txtUName2.setSelectedIndex(0);
HtmlTextInput zipCode2= htmlpage.getHtmlElementById("origZipCode");
zipCode2.setValueAttribute("41048");
HtmlTextInput discountTxt = htmlpage.getHtmlElementById("origZipCode");
discountTxt.setValueAttribute("88");
HtmlSelect commodityClass = htmlpage.getHtmlElementById("commodityClass0");
commodityClass.setSelectedIndex(4);
HtmlTextInput txtCommodityWeight = htmlpage.getHtmlElementById("txtCommodityWeight0");
txtCommodityWeight.setValueAttribute("2645.50");
//提交表单
final HtmlPage nextPage = button.click();
// 拿不到重定向的数据了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
HtmlTextInput aaaa1= htmlpage.getHtmlElementById("startDate_dtm");
//把获得后的网页转换成document
Document document = Jsoup.parse(nextPage.asXml());
//获取值
Map stringStringMap = haveAttribute(document, 1,name1.length());
// //获得第二人的 五行和五格
// Map stringStringMap2 = haveAttribute(document, 2,name2.length());
// //获得配对文本结果
// Element e = document.select("[class=\"box_con\"]").get(2);
// Elements font = e.select("font");
// String resulText = font.text().toString();
// Elements span = e.select("span");
}
public static Map haveAttribute(Document document, Integer num,Integer nameLeng){
//保存到map
Map map = new HashMap<>();
//获得name的五行
Element e = document.select("[class=\"ups-base-layout\"]").get(0);
Element e2 = document.select("[class=\"ups-base-layout\"]").get(1);
Element e3 = document.select("[class=\"ups-base-layout\"]").get(2);
Element e4 = document.select("[class=\"ups-base-layout\"]").get(4);
Elements li = e.select("li");
//判断姓名的长度
if (nameLeng==1){
//获得第一个五行的属性
String attribute1 = li.get(9).text();
map.put("attribute1",attribute1);
}
if (nameLeng==2){
//获得第一个五行的属性
String attribute1 = li.get(9).text();
//获得第二个五行的属性
String attribute2 = li.get(14).text();
map.put("attribute1",attribute1);
map.put("attribute2",attribute2);
}
if (nameLeng==3){
//获得第一个五行的属性
String attribute1 = li.get(9).text();
//获得第二个五行的属性
String attribute2 = li.get(14).text();
//获得第三个五行的属性
String attribute3 = li.get(19).text();
map.put("attribute1",attribute1);
map.put("attribute2",attribute2);
map.put("attribute3",attribute3);
}
if (nameLeng==4){
//获得第一个五行的属性
String attribute1 = li.get(9).text();
//获得第二个五行的属性
String attribute2 = li.get(14).text();
//获得第三个五行的属性
String attribute3 = li.get(19).text();
//获得第三个五行的属性
String attribute4 = li.get(24).text();
map.put("attribute1",attribute1);
map.put("attribute2",attribute2);
map.put("attribute3",attribute3);
map.put("attribute4",attribute4);
}
//获得name1的五格
Element el = document.select("[class=\"sort_2nd\"]").get(num-1);
String text = el.text().toString();
String[] split = text.split(" ");
String tian = split[2].toString();
String ren = split[6].toString();
String s = split[9].toString();
String[] split1 = s.split(">>");
String di = split1[1].toString();
Element els = document.select("[class=\"sort_3rd\"]").get(num-1);
String text2 = els.text().toString();
String[] split2 = text2.split(" ");
String wai = split2[1].split(">>")[1].toString();
String zong = split2[5].toString();
map.put("tian",tian);
map.put("ren",ren);
map.put("di",di);
map.put("wai",wai);
map.put("zong",zong);
return map;
}
}
请问哪有问题??
java webclient_java使用WebClient获取不到重定向的网页信息相关推荐
- htmlparser 获取html,利用htmlparser进行网页信息的抽取
这是个好东西,有相见恨晚之意,也比较容易上手,对很多的功能都进行了封装,做信息采集的朋友应该尽快使用它来做抓取工作.下面给一个完整的例子: 这是一个用来抽取某个网站中产品评论的类,有兴趣的朋友可以仔细 ...
- 【技术应用】java通过url爬虫获取公众号文章内容
[技术应用]java通过url爬虫获取公众号文章内容 一.前言 二.解决思路 三.爬虫工具 四.代码实现 1.爬取公众号文章 2.爬取CSDN文章 五.总结 一.前言 平时在微信或者钉钉发送消息时,会 ...
- java 根据ip获取mac地址_利用java如何根据IP获取mac地址
利用java如何根据IP获取mac地址 发布时间:2020-11-25 15:49:42 来源:亿速云 阅读:74 作者:Leah 利用java如何根据IP获取mac地址?针对这个问题,这篇文章详细介 ...
- c# typescript_在任何IDE中从C#,Java或Python代码获取TypeScript接口的简单方法
c# typescript by Leonardo Carreiro 莱昂纳多·卡雷罗(Leonardo Carreiro) 在任何IDE中从C#,Java或Python代码获取TypeScript接 ...
- 因为 Java 和 Php 在获取客户端 cookie 方式不同引发的 bug
遇到个 Java 和 Php 在获取客户端 cookie 方式不同导致跨系统的问题.所以写了这篇博客梳理下相关知识. 实验 下面通过两个简单的实验,来看Java和Php在获取web请求中的cookie ...
- java计算两个文本框的值,java 文本框自动获取另外两个文本框的值的乘积
java 文本框自动获取另外两个文本框的值的乘积 关注:282 答案:2 手机版 解决时间 2021-02-25 21:59 提问者壹玍徴戰何亽陪 2021-02-25 13:53 在panel里 ...
- java过滤html标签获取纯文本信息
转载自 java过滤html标签获取纯文本信息 package com.lyt.base.util;import java.util.regex.Pattern;public class Filter ...
- java中nonce_java如何获取微信timestamp,nonceStr,signature参数
java如何获取微信timestamp,nonceStr,signature参数 发布时间:2021-01-18 11:46:12 来源:亿速云 阅读:57 作者:小新 这篇文章主要介绍java如何获 ...
- Java 获取 URL响应头的日期信息
我们用 HttpURLConnection 的 httpCon.getDate() 方法来获取 URL响应头的日期信息 代码如下 import java.net.HttpURLConnection; ...
最新文章
- 如何通过三视图判断立方体个数_如何看股票市盈率?怎么通过市盈率判断股票?...
- 利用逆矩阵解线性方程组_QR方法求解矩阵所有特征值(一)
- 分享HTML5 canvas 的总结
- 【深度学习】遗传算法优化GAN
- 我们越来越浮躁的心靠什么去滋润
- LeetCode 318. 最大单词长度乘积(位运算)
- karaf中利用Bundle引入外部log4j配置文件
- Spark dataframe【KV格式】模拟实现Map操作
- C Primer Plus学习笔记(二)
- linux下挂载ntfs分区错误解决方法
- Java Web开发实战(一)—JDK安装及环境变量配置
- w ndoWs8pE模式下载,Windows pe下载_Windows pe免费版(Windows PE工具集合)8.2 - 系统之家...
- 打开图片默认是WPS图片,还原成win7自带的图片查看方式
- Android 通过应用设置系统日期和时间的方法
- 字节码层面理解枚举类Enum在switch中的使用
- 安装win7时,显示缺少所需的CD/DVD驱动器设备驱动程序
- CMD打开Git Bash
- javax.persistence.EntityNotFoundException: Unable to find 类 with id ?
- 烤仔TVのCCW | 区块链中的 Merkle 树
- 基于springboot的智能美食点餐配送系统