2016-07-28 2 views
3

Это мой код для разделения URL-адреса, но у этого кода есть проблемы. Все ссылки отображаются с двойным словом, например www.utem.edu.my/portal/portal. слова/портал/портал всегда двойные по любой ссылке. Любое предложение мне извлекать ссылки на веб-странице?Как разделить URL-адрес?

public String crawlURL(String strUrl) { 
    String results = ""; // For return 
    String protocol = "http://"; 

    // Assigns the input to the inURL variable and checks to add http 
    String inURL = strUrl; 
    if (!inURL.toLowerCase().contains("http://".toLowerCase()) && 
      !inURL.toLowerCase().contains("https://".toLowerCase())) { 
     inURL = protocol + inURL; 
    } 

    // Pulls URL contents from the web 
    String contectURL = pullURL(inURL); 
    if (contectURL == "") { // If it fails, then try with https 
     protocol = "https://"; 
     inURL = protocol + inURL.split("http://")[1]; 
     contectURL = pullURL(inURL); 
    } 

    // Declares some variables to be used inside loop 
    String aTagAttr = ""; 
    String href = ""; 
    String msg = ""; 

    // Finds A tag and stores its href value into output var 
    String bodyTag = contectURL.split("<body")[1]; // Find 1st <body> 
    String[] aTags = bodyTag.split(">"); // Splits on every tag 

    //To show link different from one another 
    int index = 0; 

    for (String s: aTags) { 
    // Process only if it is A tag and contains href 
    if (s.toLowerCase().contains("<a") && s.toLowerCase().contains("href")) { 

     aTagAttr = s.split("href")[1]; // Split on href 

     // Split on space if it contains it 
     if (aTagAttr.toLowerCase().contains("\\s")) 
      aTagAttr = aTagAttr.split("\\s")[2]; 

     // Splits on the link and deals with " or ' quotes 
     href = aTagAttr.split(((aTagAttr.toLowerCase().contains("\""))? "\"" : "\'"))[1]; 

     if (!results.toLowerCase().contains(href)) 
      //results += "~~~ " + href + "\r\n"; 

     /* 
     * Last touches to URl before display 
     *  Adds http(s):// if not exist 
     *  Adds base url if not exist 
     */ 

     if(results.toLowerCase().indexOf("about") != -1) { 
      //Contains 'about' 
     } 
     if (!href.toLowerCase().contains("http://") && !href.toLowerCase().contains("https://")) { 

      // http:// + baseURL + href 
      if (!href.toLowerCase().contains(inURL.split("://")[1])) 
       href = protocol + inURL.split("://")[1] + href; 
      else 
       href = protocol + href; 
     } 

     System.out.println(href);//debug 
+0

У вас есть 'if (! Results.toLowerCase(). Содержит (href)) // результаты + =" ~~~ "+ href +" \ r \ n ";' которые могут вызывать ошибки, потому что нет если применяется к другой части кода, вместо того, чтобы ничего не делать, потому что что-то прокомментировано ou т. – martijnn2008

ответ

4

рассмотреть использование класса URL ...

Используйте его как предложено в документации: )

public static void main(String[] args) throws Exception { 

     URL aURL = new URL("http://example.com:80/docs/books/tutorial" 
          + "/index.html?name=networking#DOWNLOADING"); 

     System.out.println("protocol = " + aURL.getProtocol()); 
     System.out.println("authority = " + aURL.getAuthority()); 
     System.out.println("host = " + aURL.getHost()); 
     System.out.println("port = " + aURL.getPort()); 
     System.out.println("path = " + aURL.getPath()); 
     System.out.println("query = " + aURL.getQuery()); 
     System.out.println("filename = " + aURL.getFile()); 
     System.out.println("ref = " + aURL.getRef()); 
    } 
} 

выход:

протокол HTTP =

орган = ex ample.com:80

хозяина = example.com

порт = 80

и т.д.

после этого вы можете взять элементы вам нужно создать новый один строку/URL:)

+0

Спасибо. :) Любое предложение об этом коде href = protocol + inURL.split (": //") [1] + href; Потому что я думаю, что эта часть вызывает двойную ссылку. Помоги мне, пожалуйста – Jenna