2017-05-12 4 views
0

Jsoup.connect()メソッドを使用してWebサイトから取得した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 Community 2017に含まれています)[1]と[Unaffiliated components] [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); 
関連する問題