2015-01-30 8 views
1

.edu 도메인을 통해 크롤링하는 웹 크롤러를 만들고 있습니다. jsoup를 사용하여 앵커 링크 용 HTML 파일을 구문 분석합니다. 그런 다음 중복 된 URL을 갖고 싶지 않기 때문에 링크를 HashSet에 추가합니다. 그러나, 내 HashSet 밖으로 인쇄 할 때 여러 URL이 중복되어 있습니다. HashSet Java의 중복 요소

내가 멤버 함수 내 씨앗 파일에서

private Set <String> url = new HashSet<String>(); 

public void jsoupParse(String htmlFile, String baseUrl){ 
    try{ 
     File input = new File(htmlFile); 
     Document doc = Jsoup.parse(input, "UTF-8", baseUrl); 
     Elements links = doc.select("a[href]"); 
     for (Element link : links) { 
      String linkHref = link.attr("abs:href"); 
      linkHref.trim(); 
      url.add(linkHref); 
     } 
     printCollection(); 
    } 
    catch(IOException e){ 
     e.printStackTrace(); 
    } 
} 

로 내 세트를 선언, 나는 4 개 URL이, 그래서 나는 4 htmlFiles 있습니다. 내가 인쇄 할 때 나는 659 개의 항목을 얻는다. 아래는 내 출력 샘플입니다. 예를 들어,이 특정 URL은 네 번 인쇄됩니다. http://diversity.mit.edu/diversity-summit-2015/

내 출력 : 자바와 같은 널리 사용되는 잘 확립 된 언어에 대한

http://web.mit.edu/admissions/ 
http://diversity.mit.edu/ 
http://newsoffice.mit.edu 
http://whereis.mit.edu 
http://diversity.mit.edu/diversity-summit-2015/ 
http://diversity.mit.edu/event/mlk-celebration-2015/ 
http://mit.edu/site/?ref=mithomepage 
http://ki.mit.edu 
http://web.mit.edu/athletics/www/ 
http://twitter.com/mit 
http://libraries.mit.edu/ 
http://web.mit.edu/faculty/ 
.... 
http://newsoffice.mit.edu 
http://strategiccommunications.ucr.edu/ 
http://hvrd.me/GmV2x 
http://diversity.mit.edu/diversity-summit-2015/ 
http://ucr.edu/ 
http://hvrd.me/IaiDY 
http://ki.mit.edu 
http://stanford.edu/academics/programs 
http://news.stanford.edu/news/2015/january/jones-students-econversation-012815.html 
http://harvard.edu/#skip 
http://campusmap.ucr.edu/?loc=HINHL 
+4

출력이 중복 된 것을 보여줄 수 있습니까? – Eran

답변

3

더 나은 프로그램의 문제를 생각한다.

URL 문자열 hashCode()toByteArray()을 인쇄하면 문제를 해결할 수 있습니다.

내 생각 엔 두

  1. 당신은 차이를 오해하는
  2. 귀하의 문자열이 포함되어 공간
  3. 귀하의 문자열이 같은 보이는 UTF-8 문자를 가지고 (제거 trim()를 사용)를 선도 및/또는 후행 그러나 다르다.
+0

첫 번째 추측의 의미를 설명해 주시겠습니까? 내 문자열에 트림을 추가했지만 여전히 동일한 결과를 얻습니다. 또한 세 번째 추측에 따라 charset UTF-16을 사용 하시겠습니까? – Ria

+0

나는 그것이 4 개의 URL 시드를 가지고 있기 때문에 흥미 롭다. 네 번 내 URL을 반복합니다. – Ria

+0

1. 당신이 안구를 통해 그것을 잘못 읽은 경우를 의미합니다 ... 만약 당신이 2로 해결할 수 없다면, 아마도 당신은 똑같지 만 다른 코드로 2 개의 다른 문자가 있음을 의미합니다. toByteArray()로 인쇄하면 원시 바이트 배열이 어떻게 다른지를 알 수 있습니다. –