【问题标题】:Getting pure html content from web page从网页获取纯 html 内容
【发布时间】:2016-09-17 18:19:58
【问题描述】:

我正在尝试从网页中检索所见即所得的 html 内容(使用 apache wicket 生成,但我认为它不在乎)。 我尝试了here 描述的解决方案,但我总是得到一个如下所示的 HTML 正文:

<body>
    <div
    style="width: 830px; height: 300px; margin: auto; margin-top: 50px;">
        <div wicket:id="rangeBar"
        style="float: left; width: 400px; height: 300px; margin-right: 30px;"
        id="rangeBar1"></div>
    </div>
</body>

我期待检索类似于我在浏览器 Web 控制台中看到的数据,例如:

<body>
    <div style="width: 830px; height: 300px; margin: auto; margin-top: 50px;">
        <div wicket:id="rangeBar" style="float: left; width: 400px; height: 300px; margin-right: 30px;" id="rangeBar1" class="shield-chart">
            <div id="shielddw" class="shield-container" style="position: relative; overflow: hidden; width: 400px; height: 300px; line-height: normal; z-index: 0; font-family: &amp; amp; #39; Segoe UI&amp;amp; #39; , Tahoma , Verdana, sans-serif; font-size: 12px;">
                <svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="300">
                    <defs>
                    <clippath id="shielddx">
                    <rect rx="0" ry="0" fill="none" x="0" y="0" width="9999" height="300" stroke-width="0.000001"></rect></clippath>
                    <clippath id="shielddy">
                    <rect fill="none" x="0" y="0" width="331" height="210"></rect></clippath>
                    <filter id="a5a87bf2-0ea3-4664-8ceb-bd50b883a117" height="120%">
                    <fegaussianblur in="SourceAlpha" stdDeviation="3"></fegaussianblur>
                    <fecomponenttransfer>
                    <fefunca type="linear" slope="0.2"></fefunca></fecomponenttransfer>
                    <femerge>
                    <femergenode></femergenode>
                    <femergenode in="SourceGraphic"></femergenode></femerge></filter></defs>
                    <rect rx="0" ry="0" fill="#2D2D2D" x="0" y="0" width="400"
                    height="300" stroke-width="0.000001"></rect>  
                      ..... 
                 </svg>
            </div>
            <div class="shield-tooltip" style="pointer-events: none"></div>
        </div>
    </div>
</body>

有没有办法在java中获取这样的内容?

谢谢, 劳拉

更新:这是我的 java 代码

HttpClientBuilder builder = HttpClientBuilder.create();
CloseableHttpClient httpclient = builder.build();
HttpGet httpget = new HttpGet(TEST_WEB_PAGE);
HttpResponse response = httpclient.execute(httpget);
InputStream content = response.getEntity().getContent();
OutputStream htmlStream = null;
File htmlFile = new File(ROOT + "etc/html/demo_apache_" + new Date() + ".html");
try {
    htmlStream = new FileOutputStream(htmlFile);
    byte[] buffer = new byte[8 * 1024];
    int bytesRead;
    while ((bytesRead = content.read(buffer)) != -1) {
        htmlStream.write(buffer, 0, bytesRead);
    }
} finally {
    if (htmlStream != null)
        htmlStream.close();
}

【问题讨论】:

  • 请提供您用于检索数据的 Java 代码。
  • 使用 Java 代码更新问题

标签: java html wicket


【解决方案1】:

页面加载后,head 标记中是否包含任何可能填充 div 的 JavaScript?

如果您使用 Java 以编程方式获取页面,则不会执行此 JavaScript。

【讨论】:

  • 我不熟悉您的项目设置,但也许您可以通过编程方式在 Web 控件中呈现页面,然后将源代码拉回其中
  • 我该怎么做?
  • 这取决于你是否使用 Android/Swing/ 其他东西。看看能不能找到相关的控件,看看文档
  • 我正在编写一个 java 后端应用程序。我必须将网页的内容保存为 pdf。
  • 项目中可以不包含前端库吗?您不需要实际显示它。
猜你喜欢
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-23
相关资源
最近更新 更多