URLからページを取得する方法をこのサンプルプログラムを示しています。その後、すべてのリンク、画像や他の補助的なコンテンツページを抽出します。そして、URLやテキスト情報を確認してください。
パラメータとしてURLを指定するには、以下のプログラムのニーズを実行します
パッケージorg.jsoup.examples。 輸入org.jsoup.Jsoup; 輸入org.jsoup.helper.Validate; 輸入org.jsoup.nodes.Document; 輸入org.jsoup.nodes.Element; 輸入org.jsoup.select.Elements。 インポートにjava.io.IOException; / ** * URLからリストのリンクにプログラム例。 * / パブリック クラスListLinks { 公共 静的 ボイドメイン(文字列[]引数)がスローにIOException { Validate.isTrue(args.length == 1、 "使用:フェッチへの供給URLを" )。 文字列のURL =引数[0]; 印刷( "フェッチ%sの..." は、url); 文書ドキュメント = Jsoup.connect(URL)に.get(); 要素リンク = doc.select( "[HREF]" )。 要素メディア = doc.select( "[SRC]" )。 要素輸入 = doc.select( "リンク[HREF]" )。 印刷( "\ nMedia:(%d)は" 、media.size()); 用(素子SRC:メディア){ 場合(src.tagName()に等しい( "IMG" )) プリント( "*%S:<%S>%SX%S(%S)" 、 src.tagName()、 SRC。"ABS:SRC")、src.attr( "幅")、src.attr( "高" )、 トリム(src.attr( "ALT")、20 ))。 他の 印刷( "*%sの<%S>"、src.tagName()、src.attr( "ABS:SRC" )); } 印刷( "\ nImports:(%d)は" 、imports.size())。 用{(輸入要素リンク) プリント( "*%、S <%S>(%S)"、link.tagName()、link.attr( "ABS:HREF")、link.attr( "REL" )); } 印刷( "\ nLinks:(%d)は" 、links.size())。 "*:<%S>(%S)"、link.attr( "ABS:HREF")、(link.text()、35トリム))。 } } プライベート 静的 ボイドプリント(文字列MSG、オブジェクト...引数){ System.out.printlnは(String.Formatの(MSG、引数))。 } プライベート 静的文字列のトリム(文字列S、INT 幅){ 場合(s.length()> 幅) リターン s.substring(0、幅-1)+ "" ; 他の リターン秒; } }
例入力結果
フェッチのhttp://news.ycombinator.com / ... メディア:( 38 ) * IMG:<http://ycombinator.com/images/y18.gif> 18×18() * IMG:<のhttp:// Yコンビネータ。 COM /画像/ s.gif> 10x1では() * IMG:<http://ycombinator.com/images/grayarrow.gif> X() * IMG:<http://ycombinator.com/images/s.gif> 0x10の() *スクリプト:<http://www.co2stats.com/propres.php?s = 1138 > * IMG:<http://ycombinator.com/images/s.gif> 15x1() * IMG:< http://ycombinator.com/images/hnsearch.png> X() * IMG:<http://ycombinator.com/images/s.gif> 25x1() * IMG:<http://mixpanel.com/ site_media /画像/ mixpanel_partner_logo_borderless.gif> X(Mixpanによる解析。) 輸入:( 2 ) *リンク<http://ycombinator.com/news.css>(スタイルシート) *リンク<http://ycombinator.com/favicon.ico>(ショートカットアイコン) リンク:( 141 ) *:<のhttp:// ycombinator.com>() *:<http://news.ycombinator.com/news>(ハッカーニュース) *:<http://news.ycombinator.com/newest>(新) *:<HTTP ://news.ycombinator.com/newcomments>(コメント) *:<http://news.ycombinator.com/leaders>(リーダー) *:<http://news.ycombinator.com/jobs>(ジョブ) *:<http://news.ycombinator.com/submit>(提出) *:<http://news.ycombinator.com/x?fnid = JKhQjfU7gW>(ログイン) *:<のhttp: //news.ycombinator.com/vote?for = 1094578&DIR =アップ&そこ=%6E%65%77%73>() * <http://www.readwriteweb.com/archives/facebook_gets_faster_debuts_homegrown_php_compiler.php?utm_source =フィードバーナー&utm_medium =フィード&utm_campaignを=フィード%3A +はReadWriteWeb +%28ReadWriteWeb%29&utm_content = ツイッター>(FacebookはPHPをスピードアップ) *: <http://news.ycombinator.com/user?id = MCxxで>(MCxxで) * <http://news.ycombinator.com/item?id = 1094578>(9件のコメント) *:<HTTP: //news.ycombinator.com/vote?for = 1094649&DIR =アップ&そこ=%6E%65%77%73 >() *:<http://groups.google.com/group/django-developers/msg/a65fbbc8effcd914 >( "タフDjangoはXHTMLを生成します。。" ) *:<のhttp://news.ycombinator。COM /ユーザー?ID =andybak>(andybak) *:<http://news.ycombinator.com/item?id = 1094649>(3件のコメント) *:<http://news.ycombinator.com/vote?for = 1093927&DIR =アップ&そこから=%6E%65%77%73 >() * <http://news.ycombinator.com/x?fnid = p2sdPLE7Ce>(詳細) *:<http://news.ycombinator.com/lists >(リスト) *:<http://news.ycombinator.com/rss>(RSS) *:<http://ycombinator.com/bookmarklet.html>(ブックマークレット) *:<のhttp:// ycombinator.com/newsguidelines.html>(ガイドライン) *:<http://ycombinator.com/newsfaq.html>(FAQ) *:<http://ycombinator.com/newsnews.html>(ニュース) * A:<のhttp://news.ycombinator。COM /アイテム?ID = 363>(機能リクエスト) *:<http://ycombinator.com>(Y Combinatorが) *:<http://ycombinator.com/w2010.html>(適用) *:<のhttp:// Yコンビネータ。 COM / lib.html>(ライブラリ) *:<http://www.webmynd.com/html/hackernews.html>() *:<http://mixpanel.com/?from = YC>()