【问题标题】:Download the entire webpage下载整个网页
【发布时间】:2014-10-19 20:12:43
【问题描述】:

有多种方法可以使用HTMLEditorKit 下载整个网页。但是,我需要下载需要滚动才能加载其全部内容的整个网页。该技术最常通过与 Ajax 捆绑的 JavaScript 来实现。

问:有没有办法欺骗目标网页,只使用 Java code,以下载其全部内容?

Q.2:如果仅使用 Java 无法做到这一点,那么结合 JavaScript 是否可以?

简单的通知,我写的:

public class PageDownload {

    public static void main(String[] args) throws Exception {
        String webUrl = "...";
        URL url = new URL(webUrl);
        URLConnection connection = url.openConnection();
        InputStream is = connection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        HTMLEditorKit htmlKit = new HTMLEditorKit();
        HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
        HTMLEditorKit.Parser parser = new ParserDelegator();
        HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
        parser.parse(br, callback, true);

        for (HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.IMG);
                iterator.isValid(); iterator.next()) {
            AttributeSet attributes = iterator.getAttributes();
            String imgSrc = (String) attributes.getAttribute(HTML.Attribute.SRC);
            if (imgSrc != null && (imgSrc.endsWith(".jpg") || (imgSrc.endsWith(".jpeg"))
                    || (imgSrc.endsWith(".png")) || (imgSrc.endsWith(".ico"))
                    || (imgSrc.endsWith(".bmp")))) {
                try {
                    downloadImage(webUrl, imgSrc);
                } catch (IOException ex) {
                    System.out.println(ex.getMessage());
                }
            }
        }

    }

    private static void downloadImage(String url, String imgSrc) throws IOException {
        BufferedImage image = null;
        try {
            if (!(imgSrc.startsWith("http"))) {
                url = url + imgSrc;
            } else {
                url = imgSrc;
            }
            imgSrc = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
            String imageFormat = null;
            imageFormat = imgSrc.substring(imgSrc.lastIndexOf(".") + 1);
            String imgPath = null;
            imgPath = "..." + imgSrc + "";
            URL imageUrl = new URL(url);
            image = ImageIO.read(imageUrl);
            if (image != null) {
                File file = new File(imgPath);
                ImageIO.write(image, imageFormat, file);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

【问题讨论】:

  • 你能举一个这样的网站/页面的例子吗?

标签: java javascript download scroll webpage


【解决方案1】:

使用 HtmlUnit 库获取所有文本和图像/css 文件。

HTMLUnit [链接] htmlunit.sourceforge.net

1) 要下载文本内容,请使用以下链接 s 上的代码

所有文字内容[链接]How to get a HTML page using HtmlUnit

特定标签如 span [link]how to get text between a specific span with HtmlUnit

2) 要获取图像/文件,请使用以下 [link] How can I tell HtmlUnit's WebClient to download images and css?

【讨论】:

    【解决方案2】:

    是的,您可以通过 Java 代码欺骗网页在本地下载。您不能通过 Java 脚本下载 HTMl 静态内容。 JavaScript 没有像 Java 提供的那样让您创建文件。

    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    
    
    public class HttpDownloadUtility {
        private static final int BUFFER_SIZE = 4096;
    
        /**
         * Downloads a file from a URL
         * @param fileURL HTTP URL of the file to be downloaded
         * @param saveDir path of the directory to save the file
         * @throws IOException
         */
        public static void downloadFile(String fileURL, String saveDir)
                throws IOException {
            URL url = new URL(fileURL);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            int responseCode = httpConn.getResponseCode();
    
            // always check HTTP response code first
            if (responseCode == HttpURLConnection.HTTP_OK) {
                String fileName = "";
                String disposition = httpConn.getHeaderField("Content-Disposition");
                String contentType = httpConn.getContentType();
                int contentLength = httpConn.getContentLength();
    
                if (disposition != null) {
                    // extracts file name from header field
                    int index = disposition.indexOf("filename=");
                    if (index > 0) {
                        fileName = disposition.substring(index + 10,
                                disposition.length() - 1);
                    }
                } else {
                    // extracts file name from URL
                    fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                            fileURL.length());
                }
    
                System.out.println("Content-Type = " + contentType);
                System.out.println("Content-Disposition = " + disposition);
                System.out.println("Content-Length = " + contentLength);
                System.out.println("fileName = " + fileName);
    
                // opens input stream from the HTTP connection
                InputStream inputStream = httpConn.getInputStream();
                String saveFilePath = saveDir + File.separator + fileName;
    
                // opens an output stream to save into file
                FileOutputStream outputStream = new FileOutputStream(saveFilePath);
    
                int bytesRead = -1;
                byte[] buffer = new byte[BUFFER_SIZE];
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }
    
                outputStream.close();
                inputStream.close();
    
                System.out.println("File downloaded");
            } else {
                System.out.println("No file to download. Server replied HTTP code: " + responseCode);
            }
            httpConn.disconnect();
        }
    }
    

    【讨论】:

    • 创新我对你提出的问题有意义吗。
    • 我现在真的很忙于做其他事情,但我会尽快(在 7 小时内)回到这个主题。在我研究您提出的解决方案之后,您的帮助将得到回报。感谢您的理解。
    • 太好了,成功了。但是,我在 9gag.com 上对其进行了测试,但并没有下载全部内容。如果滚动浏览 9gag,大约 30 秒,您将到达页面底部。到那时,您的代码提供的下载文件中不存在大量图像,并且它们的结尾 .jpg 或 .gif 不存在。我认为您的方式可能是这里唯一暴露的方式...如果不会发布更有效的代码,那么赏金将归您所有。谢谢。
    • 有一些软件提供了下载整个页面的工具,包括 css、js、图像和字体。但如果您使用的是 Java 程序,则只能下载 URL 中提供的内容(此处仅 HTML 代码)。
    【解决方案3】:

    您可以使用 Selenium Webdriver java 类来实现这一点...

    https://code.google.com/p/selenium/wiki/GettingStarted

    一般使用webdriver进行测试,但它可以模拟用户向下滚动页面,直到页面停止变化,然后您可以使用java代码将内容保存到文件中。

    【讨论】:

      【解决方案4】:

      您可以使用 IDM 的抓取器来完成。

      这应该会有所帮助: https://www.internetdownloadmanager.com/support/idm-grabber/grabber_wizard.html

      【讨论】:

        猜你喜欢
        • 2017-06-21
        • 2010-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-17
        • 1970-01-01
        • 1970-01-01
        • 2018-01-29
        相关资源
        最近更新 更多