如何解决相对URL与Jsoup?(How to resolve relative url with

2019-06-27 04:47发布

您好我有Jsoup问题。

我刮了页面,并得到了很多的URL。 他们有些是像相对URL: "../index.php""../admin""../details.php"

我使用attr("abs:href")以获得绝对URL,但是这个链接会以类似www.domain.com/../admin.php

我想知道这是否是一个错误。

是否有一种方式来获得与jsoup真正的绝对路径? 我怎样才能解决这个问题?

我也试图与absurl("href")但不工作。

Answer 1:

也是一个很好的选择是使用ABS:HREF或ABS:SRC属性:

String relHref = link.attr("href"); // == "/"
String absHref = link.attr("abs:href"); // "http://jsoup.org/"

这也有描述: http://jsoup.org/cookbook/extracting-data/working-with-urls



Answer 2:

如果element包含相对链接你得到这样的绝对链接: element.absUrl("href")

但是,你必须设置基本URI之前,你的相对链接 (如打电话。 setBaseUri("http://www.myexample.com")在您的DocumentElement )。

让您的舒尔基URI是足够长的时间!

好:

element.setBaseUri("http://www.example.com/abc/");
element.attr("href", "../b/here");

回报: http://www.example.com/b/here

坏:

element.setBaseUri("http://www.example.com/abc/");
element.attr("href", "../../b/here");

回报: http://www.example.com/../b/here

- >您的相对链接太长,你基本URI!



文章来源: How to resolve relative url with Jsoup?
标签: java url jsoup