源文件你讀取不了,你頂多讀取出百度的頁面代碼,而這些頁面代碼是后臺(tái)經(jīng)過server編譯過的,已經(jīng)全都是HTML,因?yàn)橹挥蠬TML CSS和一些動(dòng)態(tài)腳本瀏覽器認(rèn)識(shí),其他的瀏覽器不認(rèn)識(shí)
創(chuàng)新互聯(lián)建站科技有限公司專業(yè)互聯(lián)網(wǎng)基礎(chǔ)服務(wù)商,為您提供四川主機(jī)托管,高防主機(jī),成都IDC機(jī)房托管,成都主機(jī)托管等互聯(lián)網(wǎng)服務(wù)。
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
private String u;
private String encoding;
public static void main(String[] args) throws Exception {
HttpTest client = new HttpTest("", "UTF-8");
client.run();
}
public HttpTest(String u, String encoding) {
this.u = u;
this.encoding = encoding;
}
public void run() throws Exception {
URL url = new URL(u);// 根據(jù)鏈接(字符串格式),生成一個(gè)URL對(duì)象
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();// 打開URL
BufferedReader reader = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream(), encoding));// 得到輸入流,即獲得了網(wǎng)頁的內(nèi)容
String line; // 讀取輸入流的數(shù)據(jù),并顯示
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
}
根據(jù)具體問題類型,進(jìn)行步驟拆解/原因原理分析/內(nèi)容拓展等。
具體步驟如下:/導(dǎo)致這種情況的原因主要是……
傳入一個(gè)url,返回源代碼; public static String getHTML(String url){// 獲取指定URL的網(wǎng)頁,返回網(wǎng)頁內(nèi)容的字符串,然后將此字符串存到文件即可 try { URL newUrl = new URL(url); URLConnection connect = newUrl.openConnection(); connect.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); DataInputStream dis = new DataInputStream(connect.getInputStream()); BufferedReader in = new BufferedReader(new InputStreamReader(dis,"UTF-8")); String html = ""; String readLine = null; while((readLine = in.readLine()) != null) { html = html + readLine; } in.close(); return html; }catch (MalformedURLException me){ System.out.println("MalformedURLException" + me); }catch (IOException ioe){ System.out.println("ioeException" + ioe); } return null; }
只能抓取靜態(tài)的頁面源代碼,因?yàn)楹芏嗍录蜆邮绞莿?dòng)態(tài)綁定和執(zhí)行的,所以不可能獲取到執(zhí)行完后的代碼的。
public
String
getHtmlContent(String
htmlurl)
{
URL
url;
String
temp;
StringBuffer
sb
=
new
StringBuffer();
try
{
url
=
new
URL(htmlurl);
BufferedReader
in
=
new
BufferedReader(new
InputStreamReader(url.openStream(),
"gbk"));
while
((temp
=
in.readLine())
!=
null)
{
sb.append(temp);
}
in.close();
}
catch
(final
MalformedURLException
me)
{
me.getMessage();
}
catch
(final
IOException
e)
{
e.printStackTrace();
}
return
sb.toString();
}
這也需要解釋,readLine()是中斷式的唄,每次運(yùn)行到這一行都要讀取到下一行之后才會(huì)繼續(xù)后面的程序
readLine是中斷式的你沒有辦法解決,再說也不是你的問題,是對(duì)方服務(wù)器或者是網(wǎng)絡(luò)的問題,我們能做的只是設(shè)置一個(gè)timeout,時(shí)間過了,提示讀取失敗,可以試試apache的HttpClient
1.編寫useSourceViewer 類的基本框架,該類僅包括無返回值的main ()方法,該方法從參數(shù)中獲取URL,通過輸入緩沖和輸出緩沖將該URL 原碼輸出。
2.編寫useSourceViewer 類,代碼如下:
import java.net.*;
import java.io.*;
public class useSourceViewer
{
public static void main (String[] args)
{
if (args.length 0)
{
try
{
//讀入U(xiǎn)RL
URL u = new URL(args[0]);
InputStream in = u.openStream( );
// 為增加性能存儲(chǔ)輸入流
in = new BufferedInputStream(in);
// 將輸入流連接到閱讀器
Reader r = new InputStreamReader(in);
int c;
while ((c = r.read( )) != -1)
{
System.out.print((char) c);
}
Object o = u.getContent( );
System.out.println("I got a " + o.getClass().getName( ));
}
catch (MalformedURLException e)
{
System.err.println(args[0] + " is not a parseable URL");
}
catch (IOException e)
{
System.err.println(e);
}
} // end if
} // end main
} // end SourceViewer}
分享題目:java讀取網(wǎng)站源代碼,javascript獲取網(wǎng)頁源碼
標(biāo)題鏈接:http://aaarwkj.com/article34/hchipe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、動(dòng)態(tài)網(wǎng)站、營(yíng)銷型網(wǎng)站建設(shè)、服務(wù)器托管、網(wǎng)站排名、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)