java - JSoup 格式错误的 URL 异常

标签 java arraylist jsoup

我正在尝试使用 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());
           }
    }  
}  

输出

Google

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/

相关文章:

java - 如何使用 Java ArrayList 获取字符串用户输入并将值增加 +1?

java - 如何只读取一行文本文件?

java - TextArea 仅显示一次结果

Java 将时间转换为 iso_instant

java - 数组的数组列表,如何使最后一个数组的大小与列表中的其他数组有所不同

java - FileInputStream 和 OutputStream 无法在文件中读取和写入对象

java - java中线程的可变传输值

java - JSOUP 从重定向链接中获取 html 内容

java - 如何确定安全链接的下载链接?

java - 工厂模式 - 在 SQL 存储库中构建然后构建?