如何动态获取网页上的数据
由于工作需要,想做一个动态获取网站上的数据的程序,例如我想在www.24k99.com实时获取市场上的黄金价格加至我开发的一个软件上的一个表格中?请问要如何操作呢?
------解决方案--------------------public static String readHtmlFile(String urlPath){
String htmlFile= " ";
try {
URL url = new URL(urlPath);
URLConnection urlConnection = url.openConnection();
urlConnection.setAllowUserInteraction(false);
// InputStream urlStream = url.openStream();
InputStream urlStream = urlConnection.getInputStream();//.openStream();
InputStreamReader sr = new InputStreamReader(urlStream, "GBK ");
int byteRead = 0;
char[] buffer = new char[8192];
while ((byteRead = sr.read(buffer, 0, 8192)) != -1) {
//System.out.println(new String(buffer,0, byteRead));
//正则表达式判断
String str=new String(buffer,0,byteRead);
String r= "( <td align= 'center '> )(.*)( </td> ) ";
Pattern s=Pattern.compile(r);
Matcher m = s.matcher(str);
boolean T=m.matches();
if(T){
htmlFile=htmlFile+m.group(2);
}
htmlFile+=new String(buffer,0, byteRead);
}
} catch (
IOException e) {
System.out.println( "error : " + e.getMessage());
}
你只要改一下正则表达式就行了