JSoup: BaseURL in connect method

2019-09-03 07:00发布

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条回答
我欲成王,谁敢阻挡
2楼-- · 2019-09-03 07:25

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"));
}
查看更多
登录 后发表回答