JSoup: BaseURL in connect method

泪湿孤枕 提交于 2019-12-12 02:43:45

问题


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"));
}


来源:https://stackoverflow.com/questions/33466932/jsoup-baseurl-in-connect-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!