Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jsoup中的页面复制错误_Java_Html_Jsoup - Fatal编程技术网

Java Jsoup中的页面复制错误

Java Jsoup中的页面复制错误,java,html,jsoup,Java,Html,Jsoup,我构建了一个代码,用Jsoup将整个页面作为HTML下载。下载部分按预期工作。但我的问题是-当我打开下载的文件时,页面在浏览器中被复制了不止一次,我不知道出了什么问题。查看下面的代码: public class httptest { static File file; String crawlingNode; static BufferedWriter writer = null; static httptest ht; public httptest

我构建了一个代码,用Jsoup将整个页面作为HTML下载。下载部分按预期工作。但我的问题是-当我打开下载的文件时,页面在浏览器中被复制了不止一次,我不知道出了什么问题。查看下面的代码:

public class httptest {

    static File file;
    String crawlingNode;
    static BufferedWriter writer = null;
    static httptest ht;

    public httptest() throws IOException{

            file = new File(//***SET HERE YOUR TEST PATH***);   

    }

    private void GetLinks() throws IOException{

        Document doc = Jsoup.connect("http://google.com/search?q=mamamia")
                    .userAgent("Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)")
                    .cookie("auth", "token")
                    .timeout(3000)
                    .get();

        Elements links = doc.select("*");
            String crawlingNode = links.html();
                System.out.println(crawlingNode);
                    httptest.WriteOnFile(writer, crawlingNode);

    }


       private static void OpenWriter(File file){
           try {
                writer = new BufferedWriter(new FileWriter(file));

        } catch (IOException e) {

            JOptionPane.showMessageDialog(null, "Failed to open URL Writer");
                e.printStackTrace();

        }

       }

       private static void WriteOnFile(BufferedWriter writer, String crawlingNode){

           try {

                writer.write(crawlingNode);
        } catch (IOException e) {

            JOptionPane.showMessageDialog(null, "Failed to write URL Node");
                e.printStackTrace();

        }

       }


       private static void CloseWriter(BufferedWriter writer){
           try {

                writer.close();

           } catch (IOException e) {

               JOptionPane.showMessageDialog(null, "Unable to close URL Writer");
                System.err.println(e);

           }
       }

       public static void main (String[] args) throws IOException{

            ht = new httptest();
            httptest.OpenWriter(file);
            ht.GetLinks();
            httptest.CloseWriter(writer);

    }

}
代码的某些部分可能看起来很奇怪,但请记住,这是SSCCE代码版本。有什么有用的想法吗?提前感谢。

而不是:

Elements links = doc.select("*");
    String crawlingNode = links.html();
        System.out.println(crawlingNode);
            httptest.WriteOnFile(writer, crawlingNode);
使用:

我认为元素类型更复杂,更详细。我在分析此源代码时发现此代码更改:

无论如何,这个解决方案对我来说是可行的

  Element links = doc.select("*").first();
            String crawlingNode = links.html();
                System.out.println(crawlingNode);
                    httptest.WriteOnFile(writer, crawlingNode);