2017-05-12 2 views
0

Jsoup.connect() 방법을 사용하여 웹 사이트에서 가져온 HTML 소스가 있습니다. 그 HTML 소스에서 코드의 조각이되어 다음 (링크 : https://docs.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-community는)Jsoup H2 태그 앞에 모두 제거

..... 
<p>When you set dependencies in your VSIX manifest, you must specify Component IDs 
    only. Use the tables on this page to determine our minimum component dependencies. 
    In some scenarios, this might mean that you specify only one component from a workload. 
    In other scenarios, it might mean that you specify multiple components from a single 
    workload or multiple components from multiple workloads. For more information, see 
    the 
<a href="../extensibility/how-to-migrate-extensibility-projects-to-visual-studio-2017" data-linktype="relative-path">How to: Migrate Extensibility Projects to Visual Studio 2017</a> page.</p> 
..... 
<h2 id="visual-studio-core-editor-included-with-visual-studio-community-2017">Visual Studio core editor (included with Visual Studio Community 2017)</h2> 
..... 
<h2 id="see-also">See also</h2> 
..... 

내가 jsoup을 사용하고 싶은 나는 <h2 id="visual-studio-core-editor-included-with-visual-studio-community-2017">Visual Studio core editor (included with Visual Studio Community 2017)</h2>

, 그리고 모든 것을하기 전에 모든 하나의 HTML 조각을 제거 할 것이다

 try { 
      document = Jsoup.connect(Constants.URL).get(); 
     } 
     catch (IOException iex) { 
      iex.printStackTrace(); 
     } 
     document = Parser.parse(document.toString().replaceAll(".*?<a href=\"workload-and-component-ids\" data-linktype=\"relative-path\">Visual Studio 2017 Workload and Component IDs</a> page.</p>", "") , Constants.URL); 
     document = Parser.parse(document.toString().replaceAll("<h2 id=\"see-also\">See also</h2>?.*", "") , Constants.URL); 
     return null; 
: <h2 id="see-also">See also</h2>

(포함) 후 나는 나를 위해이 같은 솔루션,하지만이 거의 didnt 한 일이

도움이 될 것입니다.

+0

자세히 설명해 주시겠습니까? 왜 당신이 선택하고 특정 클래스 또는 태그를 제거하지 않는 제거 싶었어? 그렇지 않으면 원하는 특정 태그 만 선택할 수 있습니다. – soorapadman

+0

내가받는 html 페이지는 복잡한 구조를 가지고 있습니다. 그것은 다른 태그 사이에 많은 태그를 가지고 있습니다. 너 자신을 확인할 수있어. 페이지 src는 https://docs.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-community입니다. "Visual Studio 핵심 편집기 (Visual Studio 커뮤니티 2017 포함)"[1]과 "비 제휴 구성 요소"[2] 사이에서 HTML 소스를 다뤄야합니다. 그러나 나는 [1] 이전에 아무것도 대체하는 방법을 알 수 없으며 [2] 테이블 다음에 어떤 것도 대체 할 수 없습니다. 제발 사이트를 확인하십시오 – Sparker0i

+0

또한 JSOUP을 사용하여 이것을하고 싶었고 웹 사이트에서 HTML을 파싱했기 때문에 상수 파일이 아니 었습니다 (여러분이 말하는 것처럼) – Sparker0i

답변

1

간단한 방법은 페이지의 전체 HTML을 문자열로 가져와 필요한 부분의 부분 문자열을 만들고 해당 부분 문자열을 다시 jsoup로 구문 분석하는 것입니다.

 Document doc = Jsoup.connect("https://docs.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-community").get(); 
     String html = doc.html().substring(doc.html().indexOf("visual-studio-core-editor-included-with-visual-studio-community-2017")-8, 
              doc.html().indexOf("unaffiliated-components")-8); 
     Document doc2 = Jsoup.parse(html); 
     System.out.println(doc2); 
+0

고마워요. 코드를 조금 수정하면 내가 원하는 것을 얻을 수 있습니다. 너는 내가 큰 문제를 해결하는 것을 정말로 도왔다. – Sparker0i

1

위의 @eritrean의 답을 약간 변경합니다. 필요한 출력을 얻으려면 약간의 수정이 필요합니다.

document = Jsoup.parse(document.html().substring(document.html().indexOf("visual-studio-core-editor-included-with-visual-studio-community-2017")-26, 
       document.html().indexOf("see-also")-8)); 
System.out.println(document);