Java HTML Parse Jsoup parse(URL url, int timeout)

Here you can find the source of parse(URL url, int timeout)

Description

parse

License

Apache License

Parameter

Parameter Description
url a parameter
timeout a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static Document parse(URL url, int timeout) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.net.URL;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

public class Main {
    /**/*from  w w  w  .  j  av a 2 s  .c o  m*/
     *
     * @param url
     * @param timeout
     * @return
     * @throws IOException
     */
    public static Document parse(URL url, int timeout) throws IOException {
        Document doc = null;

        final int LIMIT = 10;
        final int LIMIT_SLEEP = 2;

        int iteration = 0;

        while (null == doc) {
            try {
                doc = Jsoup.connect(url.toString()).timeout(timeout).referrer("http://www.google.com/search")
                        .userAgent(
                                "Mozilla/6.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/3.0.0.0")
                        .get();
            } catch (IOException e1) {
                System.out.println("TIMEOUT: refetching: " + iteration);
                if (iteration > LIMIT) {
                    throw e1;
                }

                if (iteration > LIMIT_SLEEP) {
                    sleep(timeout * iteration);
                }

                iteration++;
            }
        }
        return doc;
    }

    /**
     *
     * @param delay
     */
    public static void sleep(int delay) {
        try {
            Thread.sleep(delay);
        } catch (Exception e) {
            //...
        }
    }
}

Related

  1. parse(final String html)
  2. parse(InputStream input, String documentIRI, String encoding)
  3. parse(String html)
  4. parse(String html)
  5. parse(String html)
  6. parseByteData(ByteBuffer byteData, String charsetName, String baseUri, Parser parser)
  7. parseEmail(String content)
  8. parseFile(String filePath)
  9. parseInfoBody(Element element)