![](/img/trans.png)
[英]How to get just first element in html fie By using jsoup in JAVA
[英]how just get url with html type with jsoup
我只想下載內容類型為“ text / html”的網站,而不下載pdf / mp4 / rar ...文件
現在我的代碼是這樣的:
Connection connection = Jsoup.connect(linkInfo.getLink()).followRedirects(false).validateTLSCertificates(false).userAgent(USER_AGENT);
Document htmlDocument = connection.get();
if (!connection.response().contentType().contains("text/html")) {
return;
}
是否沒有類似的東西:
Jsoup.connect(linkInfo.getLink()).contentTypeOnly("text/html");
如果您的意思是在實際下載文件之前需要了解文件是否為HTML的方法,則可以使用HEAD請求。 這將僅請求標題,因此您可以在實際下載文件之前檢查它是否為text/html
。 您所使用的方法並沒有真正起作用,因為您正在下載文件並在檢查之前將其解析為HTML,這將在非HTML文件上引發異常。
Connection connection = Jsoup.connect(linkInfo.getLink())
.method(Connection.Method.HEAD)
.validateTLSCertificates(false)
.followRedirects(false)
.userAgent(USER_AGENT);
Connection.Response head = connection.execute();
if (!head.contentType().contains("text/html")) return;
Document html = Jsoup.connect(head.url())
.validateTLSCertificates(false)
.followRedirects(false)
.userAgent(USER_AGENT)
.get();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.