如何从jsoup文档中获取“url”?

huangapple go评论69阅读模式
英文:

How to get "url" from a jsoup document?

问题

假设我使用以下代码建立了一个连接:

Document document = Jsoup.connect(url)
                  .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36")
                  .timeout(120*1000)
                  .followRedirects(true)
                  .get();

现在我想提取使用 Jsoup 建立连接的 URL,我该如何做呢?

英文:

Suppose if I made a connection using the following code :

   Document document = Jsoup.connect(url)
                      .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36")
                      .timeout(120*1000)
                      .followRedirects(true)
                      .get();

and now I want to extract the url with which I made connection with Jsoup, how can I do that ?

答案1

得分: 2

唯一可以从jsoup document 中获取的URL(在这种情况下为 String 类型)是使用 location() 方法,注意可能的重定向,可能会得到与起始URL不同的URL:

获取解析此文档的URL。如果起始URL是重定向,则会返回文档所服务的最终URL。

英文:

The only url (in this case as a String) you can obtain from a jsoup document is using the location() method, being aware of possible redirects that can bring an url different from the starting url:

> Get the URL this Document was parsed from. If the starting URL is a
> redirect, this will return the final URL from which the document was
> served from.

huangapple
  • 本文由 发表于 2020年8月28日 16:51:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63630517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定