【问题标题】:Java getting HTML frameJava获取HTML框架
【发布时间】:2018-06-22 20:05:45
【问题描述】:

我正在为此 website 开发一个 Java 项目。我需要访问网页框架的 HTML 代码,但我似乎无法做到。

<html>
<HEAD>

<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM WebSphere Studio">
<META http-equiv="Content-Style-Type" content="text/css">

<TITLE>CONSULTA CAUSAS - Versión 1.15.0.0</TITLE>
</HEAD>
  <frameset rows="120,0*" cols="*" framespacing="0" frameborder="NO" border="0">
    <frame src="/CIVILPORWEB/jsp/General/CIV_GRL_HeadAutoconsulta.jsp" name="head" scrolling="NO" noresize marginheight="144" marginwidth="0">  
    <frameset rows="*" cols="174,*" framespacing="0" frameborder="NO" border="0">
        <frame src="/CIVILPORWEB/jsp/Menu/Aplicacion/CIV_MNU_AtPublico.jsp" name="menu" scrolling="NO" noresize marginwidth="144">
        <frame src="/CIVILPORWEB/jsp/General/CIV_GRL_Body.jsp" name="body">
    </frameset>
  </frameset>
  <noframes>
    <body>
      <p></p>
    </body>
  </noframes>   
</html>

有什么方法可以使用 HtmlUnit 访问它吗?或者可能是另一个 Java 模块?

【问题讨论】:

    标签: java html frame htmlunit


    【解决方案1】:

    这适用于最新版本的 HtmlUnit。要获取 (X)Html,您可以使用 asXml() 而不是 asText()。

        String url = "http://civil.poderjudicial.cl/CIVILPORWEB/?";
    
        try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_52)) {
            // this page has JS errors even in real browsers; ignore them like real browsers do
            webClient.getOptions().setThrowExceptionOnScriptError(false);
    
            final HtmlPage page = webClient.getPage(url);
            // System.out.println("----------------");
            // System.out.println(page.asXml());
            // System.out.println("----------------");
            // System.out.println(page.asText());
    
            final HtmlPage head = (HtmlPage) page.getFrameByName("head").getEnclosedPage();
            System.out.println("- head ---------------");
            System.out.println(head.asText());
    
            final HtmlPage menu = (HtmlPage) page.getFrameByName("menu").getEnclosedPage();
            System.out.println("- menu ---------------");
            System.out.println(menu.asText());
    
            final HtmlPage body = (HtmlPage) page.getFrameByName("body").getEnclosedPage();
            System.out.println("- body ---------------");
            System.out.println(body.asText());
        }
    

    如果您需要从服务器为每一帧发送的真实字节,您可以执行以下操作

        body.getWebResponse().getContentAsString();
    

    或

        body.getWebResponse().getContentAsStream();
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2014-02-14
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多