JSoup: BaseURL in connect method

2019-09-03 06:37发布

问题:

I have this code:

String sitePath = "http://www.google.net/";
Document doc = Jsoup.connect(sitePath).get();

Elements elements = doc.select("body");
manipulateElements(elements);

long before = System.currentTimeMillis();

File fileDir = new File("google.html");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileDir), "UTF8"));

out.write(doc.toString());
out.flush();
out.close();

How can I put absolute url for all data (style, img...)?

回答1:

Hope it will be help you.

Images.

Elements imgElements = doc.select("img");
for (Element element : imgElements) {
    element.attr("src", element.attr("abs:src"));
}

Links

Elements hrefElements = doc.select("a");
for (Element element : hrefElements) {
    element.attr("href", element.attr("abs:href"));
}       

Styles

Elements linkElements = doc.head().select("link");
for (Element element : linkElements) {
    element.attr("href", element.attr("abs:href"));
}