打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
9. Working with URLs

Working with URLs

Problem

You have a HTML document that contains relative URLs, which you need to resolve to absolute URLs.

Solution

  1. Make sure you specify a base URI when parsing the document (which is implicit when loading from a URL), and
  2. Use the abs: attribute prefix to resolve an absolute URL from an attribute:
Document doc = Jsoup.connect("http://jsoup.org").get(); 
Element link = doc.select("a").first(); 
String relHref = link.attr("href"); // == "/" 
String absHref = link.attr("abs:href"); // "http://jsoup.org/"

Description

In HTML elements, URLs are often written relative to the document's location: <a href="/download">...</a>. When you use the Node.attr(String key) method to get a href attribute, it will be returned as it is specified in the source HTML.

If you want to get an absolute URL, there is a attribute key prefix abs: that will cause the attribute value to be resolved against the document's base URI (original location): attr("abs:href")

For this use case, it is important to specify the base URI when parsing the document.

If you don't want to use the abs: prefix, there is also a method Node.absUrl(String key) which does the same thing, but accesses via the natural attribute key.

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Jsoup解析HTML实例及文档方法详解
Extract Links From a HTML Page Using Delphi
jsoup 的选择器一览表 - 开源中国社区
Jsoup选择器规则
java简单爬虫----Jsoup
谁说只有Python能写爬虫?Javaer 转身甩出这个框架:给爷爬!
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服