我正在尝试使用 JSoup 打开我存储在名为 arrayLinks 的 ArrayList 中的链接列表。当我运行代码以打开 ArrayList 中的链接时,出现格式错误的 URL 异常。但是,如果我使用格式错误的链接并将它们硬编码到应用程序中,则不会出现任何错误。我曾尝试使用 StringFormatters 或 UTF-8 查看其他几篇文章,但似乎没有任何效果。任何建议将不胜感激。谢谢!
无效的代码:
article = Jsoup.connect(arrayLinks.get(i)).get()
错误:
Caused by: java.net.MalformedURLException: no protocol: "https://www.sbnation.com/college-football-recruiting/2014/7/3/5715252/cordell-broadus-recruit-scouting-report-sure-handed-receiver"
at java.base/java.net.URL.<init>(URL.java:627)
at java.base/java.net.URL.<init>(URL.java:523)
at java.base/java.net.URL.<init>(URL.java:470)
at org.jsoup.helper.HttpConnection.url(HttpConnection.java:132)
有效代码:
article = Jsoup.connect("https://www.sbnation.com/college-football-recruiting/2014/7/3/5715252/cordell-broadus-recruit-scouting-report-sure-handed-receiver").get()
最佳答案
这非常适合我。
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import java.util.ArrayList;
public class WebScraping{
public static void main( String[] args ) throws IOException{
ArrayList<String> arrayLinks = new ArrayList<String>();
arrayLinks.add("https://www.google.com");
arrayLinks.add("https://www.youtube.com");
arrayLinks.add("https://www.facebook.com");
arrayLinks.add("https://www.sbnation.com/college-football-recruiting/2014/7/3/5715252/cordell-broadus-recruit-scouting-report-sure-handed-receiver");
for(int i=0; i<arrayLinks.size(); i++) {
Document doc = Jsoup.connect(arrayLinks.get(i)).get();
System.out.println(doc.title());
}
}
}
输出
YouTube
Facebook - ??? ?? ?? ???? ?? ????
Cordell Broadus recruit scouting report: Sure-handed receiver - SBNation.com
我认为您没有将 ArrayList 定义为 String 类型,这就是您收到格式错误的 url 异常的原因。
关于java - JSoup 格式错误的 URL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50030288/