网页中抓取数据

下面写个例子,实现从网页中抓取数据。

这个例子中,只是从网页中获取了数据,但是没有进行任何处理,只是将数据保存到一个txt文件中。

该例子是在android工程中写的。

package com.example.creepertest;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class Controller
{
public static final String SD_LOTTERY_URL = 
"http://www.sdticai.com/zjhfx/cpmain.asp?cptype=115";

public static final String SD_LOTTERY_FILE_PATH = "/data/data/com.example.creepertest/test.txt";

private BufferedWriter mBufferedWriter = null;
private BufferedReader mBufferedReader = null;

public Controller(){
Runnable runnable = new Runnable(){
@Override
public void run() {
captureHtml();
}
};
Thread thread = new Thread(runnable);
thread.start();

}

private void captureHtml(){
try{
URL sdLotterUrl = new URL(SD_LOTTERY_URL);
HttpURLConnection httpConn = 
(HttpURLConnection) sdLotterUrl.openConnection();


InputStreamReader inputStreamReader = 
new InputStreamReader(httpConn.getInputStream(), "utf-8");
mBufferedReader = new BufferedReader(inputStreamReader);  
 
OutputStream outputStream = 
new FileOutputStream(SD_LOTTERY_FILE_PATH,true);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
mBufferedWriter = new BufferedWriter(outputStreamWriter); 
 
String lineStr = null;
while(true){
lineStr = mBufferedReader.readLine();
if(lineStr != null){
mBufferedWriter.write(lineStr);
mBufferedWriter.newLine();
mBufferedWriter.flush();
}
else
break;
}
 
}
catch (MalformedURLException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}

finally{
try {
if(mBufferedWriter != null)
mBufferedWriter.close();

if(mBufferedReader != null)
mBufferedReader.close();
} catch (Exception exception){
exception.printStackTrace();
}
   }
}
}

网页中抓取数据,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。