How do I fix the MalformedURLException here?

时间:2019-04-08 13:46:03

标签: java

I want the program to print out the text of each url

HtmlPage points = (HtmlPage) webClient.getPage("https://www.hockey-reference.com/leaders/points_career.html");
String pointsPageContent=points.asText();

HtmlPage assists = (HtmlPage) webClient.getPage("https://www.hockey-reference.com/leaders/assists_career.html");
String assistsPageContent=assists.asText();




final HtmlPage page1 = webClient.getPage(pointsPageContent);

final HtmlPage page2 = webClient.getPage(assistsPageContent);




if(input == "Points") {
    System.out.println(page1);

}

else if(input == "Assists") {
System.out.println(page2);
}

else {
    System.out.println("not a valid entry, please type either 'Points' or 'Assists' to see the stats for the corresponding category");
}

I'm supposed to get the text for each URL, but it's giving me the MalformedURLException instead.

1 个答案:

答案 0 :(得分:1)

正如@JonSkeet所说,发生此错误是因为您将页面内容当作URL来获取。

    try {
        HtmlPage points = (HtmlPage) webClient.getPage("https://www.hockey-reference.com/leaders/points_career.html");
        String pointsPageContent = points.asText();

        HtmlPage assists = (HtmlPage) webClient.getPage("https://www.hockey-reference.com/leaders/assists_career.html");
        String assistsPageContent = assists.asText();

        if (input.equals("Points")) {
            System.out.println(pointsPageContent);

        } else if (input.equals("Assists")) {
            System.out.println(assistsPageContent);
        } else {
            System.out.println("not a valid entry, please type either 'Points' or 'Assists' to see the stats for the corresponding category");
        }
    } catch (IOException ex) {
        ex.printStackTrace(); // Capture and treat errors
    }

只需将页面内容与所需的输入进行比较。