开发者

How to extract full URLs from all paragraphs in a webpage using jsoup

开发者 https://www.devze.com 2023-02-20 11:53 出处:网络
How do I extract full URL\'s from all paragraphs on a web page using jsoup?I am able to extract only the relative URL\'s.

How do I extract full URL's from all paragraphs on a web page using jsoup? I am able to extract only the relative URL's.

Expected: http://fr.wikipedia.org/wiki/Husni_al-Zaim

Actual: /Husni_al-Zaim

My Code:

    Elements links = doc.select("p");
    Elements linkss = links.select("a");

    for (Element link : linkss) {
        if (link.开发者_运维技巧text().matches("^[A-Z].+") == true) {
            list.add(new NamedLink(link.attr("href"), link.text()));
        }
    }


Use .absUrl("href") instead of .attr("href"). This only works when you get the document from a webpage or parse the full file from disk (and thus do not massage portions from HTML to text and back as in your example).

Document document = Jsoup.connect("http://stackoverflow.com").get();
Elements paragraphLinks = document.select("p a");
for (Element paragraphLink : paragraphLinks) {
    String absUrl = paragraphLink.absUrl("href");
    // ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消