URLからHTMLを取得するプログラム(Java)
URLの指定先からHTMLを取ってきて表示するプログラム.
import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.UnknownHostException; import javax.swing.JTextArea; public class html_getter { public static void main(String[] args){ Object o1 = null; try { o1 = new Getter5(new URL("http://www.google.com/")); } catch (MalformedURLException e) { // TODO 自動生成された catch ブロック e.printStackTrace(); } ((Getter5)o1).Show(); } } class Getter5{ private String charset = "Shift_JIS"; private JTextArea htmlArea; public Getter5(URL url){ htmlArea = new JTextArea(); // Webページを読み込む try { // 接続 URLConnection uc = url.openConnection(); // HTMLを読み込む BufferedInputStream bis = new BufferedInputStream(uc.getInputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(bis, charset)); htmlArea.setText("");//初期化 String line; while ((line = br.readLine()) != null) { htmlArea.append(line + "\n"); } } catch (MalformedURLException ex) { htmlArea.setText("URLが不正です。"); ex.printStackTrace(); } catch (UnknownHostException ex) { htmlArea.setText("サイトが見つかりません。"); } catch (IOException ex) { ex.printStackTrace(); } } public void Show(){ System.out.println(htmlArea.getText()); } }
結果
<html><head><meta http-equiv="content-type" content="text/html; charset=Shift_JIS"><title>Google</title><style>body,td,a,p,.h{font-family:arial,sans-serif}.h{font-size:20px}.h{color:#3366cc}.q{color:#00c}.ts td{padding:0}.ts{border-collapse:collapse}</style><script> <!-- window.google={kEI:"8NfwRsSPIZu6sALtmMC2Bg",kEXPI:"17259",kHL:"ja"};function sf(){document.f.q.focus();} // --> </script> </head><body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000 onload="sf();if(document.images){new Image().src='/images/nav_logo3.png'}" topmargin=3 marginheight=3><div align=right id=guser style="font-size:84%;padding:0 0 4px" width=100%><nobr><a href="/url?sa=p&pref=ig&pval=3&q=http://www.google.co.jp/ig%3Fhl%3Dja&usg=AFQjCNECKWsVDdzBrIsAt5Qd4pt4gS64Mw">iGoogle</a> | <a href="https://www.google.com/accounts/Login?continue=http://www.google.co.jp/&hl=ja">ログイン</a></nobr></div><center><br clear=all id=lgpd><img alt="Google" height=110 src="/intl/ja_jp/images/logo.gif" width=276><br><br><form action="/search" name=f><style>#lgpd{display:none}</style><script defer><!-- //--> </script><table border=0 cellspacing=0 cellpadding=4><tr><td nowrap><font size=-1><b>ウェブ</b> <a class=q href="http://images.google.co.jp/imghp?oe=Shift_JIS&hl=ja&tab=wi">画像</a> <a class=q href="http://news.google.co.jp/nwshp?oe=Shift_JIS&hl=ja&tab=wn">ニュース</a> <a class=q href="http://maps.google.co.jp/maps?oe=Shift_JIS&hl=ja&tab=wl">地図</a> <a class=q href="http://groups.google.co.jp/grphp?oe=Shift_JIS&hl=ja&tab=wg">グループ</a> <b><a href="/intl/ja/options/" class=q>more »</a></b></font></td></tr></table><table cellpadding=0 cellspacing=0><tr valign=top><td width=25%> </td><td align=center nowrap><input name=hl type=hidden value=ja><input type=hidden name=ie value="Shift_JIS"><input maxlength=2048 name=q size=55 title="Google 検索" value=""><br><input name=btnG type=submit value="Google 検索"><input name=btnI type=submit value="I'm Feeling Lucky"></td><td nowrap width=25%><font size=-1> <a href=/advanced_search?hl=ja>検索オプション</a><br> <a href=/preferences?hl=ja>表示設定</a><br> <a href=/language_tools?hl=ja>言語ツール</a></font></td></tr><tr><td align=center colspan=3><font size=-1><span style="text-align:left"><input id=all type=radio name=lr value="" checked><label for=all> ウェブ全体から検索 </label><input id=il type=radio name=lr value="lang_ja"><label for=il> 日本語のページを検索 </label></span></font></td></tr></table></form><br><font size=-1><font color=red>New!</font> <a href=http://www.google.com/a/help/intl/ja/admins/video_tour.html#utm_source=hpp_lp&utm_medium=et&utm_campaign=ja>Google Apps</a> - 企業や学校に最適な Google サービス</font><br><br><br><font size=-1><a href="/intl/ja/ads/">広告掲載</a> - <a href="/services/">ビジネス ソリューション</a> - <a href="/intl/ja/about.html">Google について</a> - <a href=/intl/ja/jobs/>人材募集</a> - <a href=http://www.google.com/ncr>Google.com in English</a></font><p><font size=-1>©2007 Google</font></p></center></body></html>