真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

網(wǎng)頁(yè)源代碼抓取java java抓取網(wǎng)頁(yè)內(nèi)容

java中如何根據(jù)一個(gè)網(wǎng)址獲得該網(wǎng)頁(yè)的源代碼?

package test;

創(chuàng)新互聯(lián)建站從2013年成立,先為宿豫等服務(wù)建站,宿豫等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為宿豫企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java點(diǎn)虐 .HttpURLConnection;

import java點(diǎn)虐 .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();// 打開(kāi)URL

BufferedReader reader = new BufferedReader(new InputStreamReader(

urlConnection.getInputStream(), encoding));// 得到輸入流,即獲得了網(wǎng)頁(yè)的內(nèi)容

String line; // 讀取輸入流的數(shù)據(jù),并顯示

while ((line = reader.readLine()) != null) {

System.out.println(line);

}

}

}

根據(jù)具體問(wèn)題類型,進(jìn)行步驟拆解/原因原理分析/內(nèi)容拓展等。

具體步驟如下:/導(dǎo)致這種情況的原因主要是……

java程序讀取一個(gè)url頁(yè)面的源代碼

傳入一個(gè)url,返回源代碼; public static String getHTML(String url){// 獲取指定URL的網(wǎng)頁(yè),返回網(wǎng)頁(yè)內(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; }

Java訪問(wèn)指定URL并獲取網(wǎng)頁(yè)源代碼

1.編寫(xiě)useSourceViewer 類的基本框架,該類僅包括無(wú)返回值的main ()方法,該方法從參數(shù)中獲取URL,通過(guò)輸入緩沖和輸出緩沖將該URL 原碼輸出。

2.編寫(xiě)useSourceViewer 類,代碼如下:

import java點(diǎn)虐 .*;

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}


網(wǎng)頁(yè)題目:網(wǎng)頁(yè)源代碼抓取java java抓取網(wǎng)頁(yè)內(nèi)容
轉(zhuǎn)載注明:http://weahome.cn/article/ddgposo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部