如何动态获取网页上的数据

bee6803 2007-02-02 10:28:34
由于工作需要,想做一个动态获取网站上的数据的程序,例如我想在www.24k99.com实时获取市场上的黄金价格加至我开发的一个软件上的一个表格中?请问要如何操作呢?
...全文
380 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
InMyLove 2007-04-24
  • 打赏
  • 举报
回复
顶~~~
zdblzwj 2007-04-24
  • 打赏
  • 举报
回复
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());
}

你只要改一下正则表达式就行了
bee6803 2007-02-02
  • 打赏
  • 举报
回复
UP

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧