2014-07-25 19 views
0

這是我爲了從網頁上讀取五行而寫的一段代碼。但不是內容我最終得到的HTML標籤。我試圖找出網上,它說proxyhost和proxyport需要設置(http://docs.oracle.com/javase/tutorial/networking/urls/_setProxy.html),但我無法弄清楚應該是我的proxyhost和proxyport。我如何找到?使用java聯網訪問網頁內容

BufferedReader鍵盤; String inputLine;

keyboard = new BufferedReader(new InputStreamReader(System.in)); 

System.out.print("Please enter the name of a website "); 
System.out.flush();  /* Make sure the line is printed immediately. */ 
inputLine = keyboard.readLine(); 


URL webpage=new URL("http://"+inputLine+".com/"); 
BufferedReader br=new BufferedReader(new InputStreamReader(webpage.openStream())); 
String reading_five_lines; 
int count=0; 
while((reading_five_lines=br.readLine())!=null||count<5) 
{ 
    count++; 
System.out.println(reading_five_lines); 
} 
+0

查看您的瀏覽器設置,瞭解什麼是代理。並改用httpclient,這樣你可以處理重定向 – Leo

+0

轉到瀏覽器中的網站,並使用瀏覽器的'查看源'功能。這就是瀏覽器得到的東西,這就是你得到的。然而,瀏覽器是一個複雜的怪物,它可以接受這些內容,解釋它並使用附帶的CSS和JS內容和圖像進行渲染。 – Gimby

回答

0

我不認爲設置一個代理將幫助你在這裏。

你看到的代碼(我假設你沒有發佈輸出)是網站的文字HTML代碼。

如果您想要可讀(人性化)版本,您必須通過HTML解析器運行它。

快速谷歌搜索帶來了這個結果。

http://htmlparser.sourceforge.net/