2012-07-05 3 views
0

다음 코드는 jsoup를 사용하여 주어진 페이지에서 URL을 추출합니다. 내가 할 노력하고있어jsoup를 사용하여 https URL을 추출하십시오.

import org.jsoup.Jsoup; 
import org.jsoup.helper.Validate; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

import java.io.IOException; 

/** 
* Example program to list links from a URL. 
*/ 
public class ListLinks { 
    public static void main(String[] args) throws IOException { 

     String url = "http://shopping.yahoo.com"; 
     print("Fetching %s...", url); 

     Document doc = Jsoup.connect(url).get(); 
     Elements links = doc.getElementsByTag("a"); 


     print("\nLinks: (%d)", links.size()); 
     for (Element link : links) { 
     print(" * a: <%s> (%s)", link.absUrl("href") /*link.attr("href")*/, trim(link.text(), 35));  
     } 
    } 

    private static void print(String msg, Object... args) { 
     System.out.println(String.format(msg, args)); 
    } 

    private static String trim(String s, int width) { 
     if (s.length() > width) 
      return s.substring(0, width-1) + "."; 
     else 
      return s; 
    } 
} 

https 사이트를 추출하는 크롤러를 구축하는 것입니다. 크롤러에 시드 링크를 제공하면 모든 https 사이트를 추출한 다음 추출 된 링크 각각을 가져 와서 특정 수의 수집 된 URL에 도달 할 때까지 동일하게 수행해야합니다.

내 질문 : 위의 코드는 주어진 페이지의 모든 링크를 추출 할 수 있습니다. https://으로 시작하는 링크를 추출해야합니다.이를 수행하려면 어떻게해야합니까?

+0

HTTP 사이트에서 자동으로 사용자를 HTTPS 사이트로 리디렉션하는 사이트가 있습니다. 그런 링크를 원하십니까? (여기서는 HTTP 요청을 시작해야하기 때문에이 경우 조금 더 어렵습니다.) – nhahtdh

+0

감사합니다. 아닙니다. 인터넷에서 https 사이트를 수집하기 만하면됩니다. –

답변

2

jsoup의 선택기를 사용할 수 있습니다. 그들은 꽤 강력합니다.

doc.select("a[href*=https]");//(This is the one you are looking for)selects if value of href contatins https 
doc.select("a[href^=www]");//selects if value of href starts with www 
doc.select("a[href$=.com]");//selects if value of href ends with .com. 

등. 올바른 방법으로 실험 해보십시오.

관련 문제