JAVA实现数据采集程序基本原理
JAVA实现数据采集程序基本原理
项目需要,非要采集别人网站的MP3,没办法只能做。。好在成功了,分享一下心得。。
同样的方法,采集FALSH,文本都可以的。。
首先要采集别人的东西就要登陆别人的网页查看源代码找到下载地址,其实就这么简单。。
那就只要先定义一个方法让程序可以自动侦测到网页的源代码了。。
程序代码:[code]public static String getWebContent(String domain){
System.out.println("开始读取内容...("+domain+")");
StringBuffer sb = new StringBuffer();
try{
java.net.URL url = new java.net.URL(domain);
BufferedReader in =
new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while((line = in.readLine()) != null){
sb.append(line);
}
in.close();
}catch(Exception e) { // Report any errors that arise
sb.append(e.toString());
System.err.println(e);
System.err.println("Usage: java HttpClient []");
}
return sb.toString();
}[/code]代码提取到超链接地址
然后村成TXT,直接用FLASHGET下载就行了
那么怎么分析出网页的超链接?
当然是用正则表达式了于是有了下面这个方法
程序代码:[code]public static String matcherStr(String str, String cp, String s){
if(str==null || str.equals("")){
return "";
}
String txt = new String();
txt = str;
if(str!=null && !str.equals("")){
txt = str;
Pattern p = Pattern.compile(cp,2); //参数2表示大小写不区分
Matcher m = p.matcher(txt);
StringBuffer sb = new StringBuffer();
int i=0;
boolean result = m.find();
//使用循环将句子里所有匹配的内容找出并替换再将内容加到sb里
while(result) {
i++;
sb.append(m.group());
sb.append(s);
//继续查找下一个匹配对象
result = m.find();
}
txt = String.valueOf(sb);
}else{
txt = "";
}
return txt;
}
[/code]一次匹配一点,不行就再匹配,直到取出网址,存在StringBuffer里面,等都弄出来了
或者打印另存成txt或者直接用程序存成TXT
只要打开FLASHGET,然后开着TXT 全选,复制,就会自动将地址添加到FLASHGET里面了
批量下吧,兄弟。。。。
1 条评论:
Hello. This post is likeable, and your blog is very interesting, congratulations :-). I will add in my blogroll =). If possible gives a last there on my blog, it is about the MP3 e MP4, I hope you enjoy. The address is http://mp3-mp4-brasil.blogspot.com. A hug.
发表评论