当前位置: 技术问答>java相关
怎么样程序实现?(如果分不够,还可以加)
来源: 互联网 发布时间:2015-06-15
本文导语: 如果我对某些网页上的内容感兴趣,怎么样才能把这些内容抓下来生成自己的页面 例如我对163上的体育新闻感兴趣,我怎么样才能把这些新闻的标题抓下来,在自己的页面上生成它 | 用URLConne...
如果我对某些网页上的内容感兴趣,怎么样才能把这些内容抓下来生成自己的页面
例如我对163上的体育新闻感兴趣,我怎么样才能把这些新闻的标题抓下来,在自己的页面上生成它
例如我对163上的体育新闻感兴趣,我怎么样才能把这些新闻的标题抓下来,在自己的页面上生成它
|
用URLConnection类,
然后getInputStream()就得到了
给你个例子,这是我写的下载软件,适用HTTP和FTP协议,抓文件和抓页面的
原理其实是一样的:
import java.io.*;
import java.net.*;
public class DownLoad
{
public static void main(String args[])
{
if (args.length != 2) System.out.println("Usage: java DownLoad URL cacheSize(KB)");
URL url = null;
URLConnection conn = null;
InputStream in = null;
FileOutputStream out = null;
int cacheSizeKB = 128;
int size = 0;
byte[] cache = null;
long startTime,endTime,totalTime;
try
{
cacheSizeKB = Integer.parseInt(args[1]);
cache = new byte[1024*cacheSizeKB];
url = new URL(/tech-qa-java/args[0]/index.html);
conn = url.openConnection();
in = conn.getInputStream();
String urlStr = url.toString();
out = new FileOutputStream(urlStr.substring(urlStr.lastIndexOf('/')+1));
size = conn.getContentLength(); //大小(byte)
System.out.println("Download "+urlStr+" ... (文件大小:"+size+"字节)");
int x = 0;
startTime = System.currentTimeMillis();
while ((x=in.read(cache)) != -1) out.write(cache, 0, x);
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
double second = totalTime/1000.0;
System.out.println("总共花费:"+totalTime+"毫秒");
System.out.println("总共花费:"+second+"秒");
System.out.println("总共花费:"+(int)second/60+":"+(int)second%60);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try { if (in != null) in.close(); } catch (Exception e) {}
try { if (out != null) in.close(); } catch (Exception e) {}
}
}
}
然后getInputStream()就得到了
给你个例子,这是我写的下载软件,适用HTTP和FTP协议,抓文件和抓页面的
原理其实是一样的:
import java.io.*;
import java.net.*;
public class DownLoad
{
public static void main(String args[])
{
if (args.length != 2) System.out.println("Usage: java DownLoad URL cacheSize(KB)");
URL url = null;
URLConnection conn = null;
InputStream in = null;
FileOutputStream out = null;
int cacheSizeKB = 128;
int size = 0;
byte[] cache = null;
long startTime,endTime,totalTime;
try
{
cacheSizeKB = Integer.parseInt(args[1]);
cache = new byte[1024*cacheSizeKB];
url = new URL(/tech-qa-java/args[0]/index.html);
conn = url.openConnection();
in = conn.getInputStream();
String urlStr = url.toString();
out = new FileOutputStream(urlStr.substring(urlStr.lastIndexOf('/')+1));
size = conn.getContentLength(); //大小(byte)
System.out.println("Download "+urlStr+" ... (文件大小:"+size+"字节)");
int x = 0;
startTime = System.currentTimeMillis();
while ((x=in.read(cache)) != -1) out.write(cache, 0, x);
endTime = System.currentTimeMillis();
totalTime = endTime - startTime;
double second = totalTime/1000.0;
System.out.println("总共花费:"+totalTime+"毫秒");
System.out.println("总共花费:"+second+"秒");
System.out.println("总共花费:"+(int)second/60+":"+(int)second%60);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try { if (in != null) in.close(); } catch (Exception e) {}
try { if (out != null) in.close(); } catch (Exception e) {}
}
}
}