英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论