- 浏览: 140562 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
jonesmillera:
Good article! Thank you for sha ...
好用的dao(spring+jpa) -
Luob.:
里面一个类都没有,有反应才怪呢
多文件上传组件SWFUpload java环境小例子 -
seaie:
谢谢博主的分享。
下载测试了下,跑起来ok,不过有3个可改进 ...
java图片压缩处理 支持gif -
angangJonny:
没法反应怎么解决的啊!
多文件上传组件SWFUpload java环境小例子 -
nookiehuihui:
点上传没反应,什么情况
多文件上传组件SWFUpload java环境小例子
在我整理完在线听歌(http://ting.faqee.com/)的模块后,剩下来的工作就是如何通过程序抓取最新的Baidu好听的歌曲,抓取的工作主要包括3个属性:歌名、歌曲在线播放地址和歌词内容(符合LRC歌词格式),目前完成歌曲和歌曲地址抓取,由于百度的歌曲地址很多通过js获取,所以歌曲地址获取我这里使用搜狗音乐搜索方便些,所有的源码如下:
package com.common.utils; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.net.URLDecoder; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.TreeSet; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.htmlparser.Node; import org.htmlparser.NodeFilter; import org.htmlparser.Parser; import org.htmlparser.filters.NodeClassFilter; import org.htmlparser.filters.OrFilter; import org.htmlparser.nodes.TextNode; import org.htmlparser.tags.LinkTag; import org.htmlparser.util.NodeList; import org.htmlparser.util.ParserException; import com.common.doc.FileOperUtils; class Song{ private String name; private String url; private String lrc; public Song(String name,String url){ this.name = name; this.url = url; this.lrc = ""; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getLrc() { return lrc; } public void setLrc(String lrc) { this.lrc = lrc; } } public class BaiduMp3 { public static String visitURL(String strUrl) { URL url = null; try { url = new URL(strUrl); } catch (MalformedURLException e) { e.printStackTrace(); } URLConnection conn = null; try { conn = url.openConnection(); conn.setDoOutput(true); } catch (IOException e) { System.out.println("e:"+e.getMessage()); } OutputStreamWriter out; try { out = new OutputStreamWriter(conn.getOutputStream(), "GBK"); out.flush(); out.close(); } catch (UnsupportedEncodingException e2) { e2.printStackTrace(); } catch (IOException e2) { e2.printStackTrace(); } // 接收返回信息 BufferedReader rd = null; try { rd = new BufferedReader( new InputStreamReader(conn.getInputStream())); return rd.readLine(); } catch (IOException e1) { e1.printStackTrace(); } return ""; } /** * 功能说明:访问指定的URL并检查返回结果。 * @param strUrl * @param successFlag 请求成功的标识,比如包含“_SUCCESS”字。 * @return */ public static String visitURL(String strUrl, String successFlag) { boolean rs = false; HttpURLConnection jconn = null; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { URL url = new URL(strUrl); jconn = (HttpURLConnection) url.openConnection(); jconn.setDoOutput(true); jconn.setDoInput(true); jconn.connect(); InputStream in = jconn.getInputStream(); byte[] buf = new byte[4096]; int bytesRead; while ((bytesRead = in.read(buf)) != -1) { byteArrayOutputStream.write(buf, 0, bytesRead); } String strRead = new String(byteArrayOutputStream.toByteArray(),"GBK"); return strRead; } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { jconn.disconnect(); try { byteArrayOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } return ""; } private static boolean isTrimEmptyOrBlank(String astr) { if ((null == astr) || (astr.length() == 0) || " ".equals(astr)) { return true; } astr = astr.trim(); if ((null == astr) || (astr.length() == 0)) { return true; } return false; } private static String getFilteredContent(String htmlContent, String reg,int i) { String content = ""; int k=1; Pattern pp = Pattern.compile(reg, Pattern.DOTALL); Matcher m = pp.matcher(htmlContent); while (m.find()) { content = m.group(); if(k++==i) break; } return content; } public static List<Song> getBaiduSongs(){ List<Song> ss = new ArrayList(); String htmlContent = visitURL("http://list.mp3.baidu.com/topso/mp3topsong.html?id=1?top2","s"); String encode = "GBK"; // System.out.println("==========================================================================="); // System.out.println(htmlContent); // System.out.println("==========================================================================="); String reg = "<table width=\"100%\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"list\">(.*?)</table>"; htmlContent = getFilteredContent(htmlContent,reg,0); //FileOperUtils.writeFile("c:\\1.html", htmlContent, false); String line = "",lineurl=""; Node anode = null; TextNode textnode = null; try { Parser parser = Parser.createParser(htmlContent, encode); NodeClassFilter textFilter = new NodeClassFilter(LinkTag.class); OrFilter lastFilter = new OrFilter(); lastFilter.setPredicates(new NodeFilter[] { textFilter }); NodeList nodeList = parser.parse(lastFilter); Node[] nodes = nodeList.toNodeArray(); for (int i = 0; i < nodes.length; i++) { anode = (Node) nodes[i]; if(anode instanceof LinkTag){ LinkTag txt = (LinkTag)anode; line = txt.getLinkText(); if(txt.getPreviousSibling()!=null){ if(txt.getPreviousSibling().toString().indexOf("(")>=0) continue; } line = txt.getLinkText(); lineurl = txt.getAttribute("href"); //System.out.println(txt.getLink()); } if (isTrimEmptyOrBlank(line)||isTrimEmptyOrBlank(lineurl)) continue; ss.add(new Song(line,getSongURL(line))); } } catch (ParserException pe) { pe.printStackTrace(); } return ss; } private static String getSongURL(String songname){ try { String ss = URLEncoder.encode(songname,"GBK"); String htmlContent = visitURL("http://so.mp3.qihoo.com/?type=0&src=s&kw="+ss,"s"); String encode = "GBK"; String reg = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">(.*?)</table>"; htmlContent = getFilteredContent(htmlContent,reg,1); String line = "",lineurl=""; Node anode = null; TextNode textnode = null; Parser parser = Parser.createParser(htmlContent, encode); NodeClassFilter textFilter = new NodeClassFilter(LinkTag.class); OrFilter lastFilter = new OrFilter(); lastFilter.setPredicates(new NodeFilter[] { textFilter }); NodeList nodeList = parser.parse(lastFilter); Node[] nodes = nodeList.toNodeArray(); for (int i = 0; i < nodes.length; i++) { anode = (Node) nodes[i]; if(anode instanceof LinkTag){ LinkTag txt = (LinkTag)anode; line = txt.getLinkText(); lineurl = txt.getAttribute("href"); if(!isTrimEmptyOrBlank(lineurl) && lineurl.startsWith("down.html")){ String s = getFilteredContent(lineurl,"u=(.*?)\\&",0); if(!s.equals("")&&s.length()>5){ s = Utils.replace(s, "u=", ""); s = Utils.replace(s, "&", ""); s = URLDecoder.decode(s,"GBK"); return s; } } } } } catch (Exception pe) { pe.printStackTrace(); } return ""; } public static void main(String[] args) throws Exception{ List<Song> ss = getBaiduSongs(); int idx = 0; for(Song s:ss){ System.out.println((++idx)+":"+s.getName()+"->"+s.getUrl()); } // String ss = getSongURL("国家"); // System.out.println(ss); // String s = URLDecoder.decode("http%3A%2F%2Fwww.yuelin.net%2F%B9%FA%BC%D2.mp3","GBK"); // System.out.println(s); } }
发表评论
-
java web 文件文件上传
2009-12-10 12:29 12964今日学习的主要内容是——文件的上传与下载,下载我们在之前的学 ... -
中文句子相似度判断源码探讨
2009-11-04 15:59 2928在我们日常的开发中, ... -
通用各类文档读写的设计与实现
2009-11-04 15:35 1033在我们日常的项目开发中,经常碰到需要读取word等文档的需求, ... -
Apache 2.2.x+Tomcat6集群配置备忘
2009-11-04 15:28 1556在我们部署Web应用时,不可避免大多都会涉及到集群问题,此文作 ... -
多文件上传组件SWFUpload java环境小例子
2009-10-16 14:16 6565包结构如下: index.jsp upload.jsp ... -
工具类
2009-07-23 17:42 1224import java.util.HashMap; impo ... -
好用的dao(spring+jpa)
2009-07-23 09:05 2519package com.itcast.service.base ... -
spring+JPA的注解(自己使用)
2009-06-10 00:48 1550依赖注入的手工装配的两种方式注解 @Resource 一般 ... -
spring的优缺点
2009-06-08 10:06 3182它是一个开源的项目,而且目前非常活跃;它基于IoC(Inver ... -
Struts优缺点
2009-06-08 09:51 1493跟Tomcat、Turbine等诸多Apa ... -
struts1与struts2的比较
2009-06-08 09:33 956特性 Struts 1 ... -
struts2+hibernate+spring分页方法
2009-05-27 13:16 3431import java.util.List; ... -
struts2+spring2+hibernate3.1应用实例
2009-05-22 18:24 12781. 组合Hibernate 与Spring ...
相关推荐
python 抓取百度云分享数据,百度云最新接口抓取分享链接。
php 抓取百度搜索结果脚本,php命令行下运行
抓取网页源码.txt抓取网页源码.txt抓取网页源码.txt抓取网页源码.txt抓取网页源码.txt抓取网页源码.txt抓取网页源码.txt
用户抓取百度地图的地址信息,关键字抓取,调用百度API ,安全可靠
java根据自定义json格式规则抓取新浪新闻、百度新闻、微博动态内容的网络爬虫源码 例子中的源码功能: 导入Hbase的jar包即可直接返回Put对象数据、可以返回map对象数据、支持自定义json格式抓取指定网页的内容、抓取...
资源名:抓取网站文章的ASP.NET程序源码 资源类型:程序源代码 源码说明: 文章抓取ASP.NET源码 最近在做一些资源采集的工作,比如采集新闻,flash,图片等,通过这个小例子,来详细的说明一下我采集资源的步骤: 1....
爬虫技术抓取网站数据 爬虫+百度贴吧图片抓取案例+源码demo
基于对知乎热榜话题的数据抓取、分析与可视化python源码+数据集.tar基于对知乎热榜话题的数据抓取、分析与可视化python源码+数据集.tar基于对知乎热榜话题的数据抓取、分析与可视化python源码+数据集.tar基于对知乎...
使用正则表达式和requests,抓取猫眼 TOP100 的电影信息
php页面抓取 抓取页面 抓页面 抓页面源码
利用R语言进行网络数据抓取,源码为抓取百度贴吧-复仇之矛吧的内容。内涵多线程代码
百度地图抓取软件,可将百度网页地图保存到本地
HttpClient get、post 请求,抓取网络数据,jar包以及源码
python抓取百度搜索的数据,智普推荐使用教程
python爬虫,抓取百度贴吧中的邮箱地址,如有环境安装问题请私信我
基于对知乎热榜话题的数据抓取、分析与可视化python源码+项目说明+数据集.zip基于对知乎热榜话题的数据抓取、分析与可视化python源码+项目说明+数据集.zip基于对知乎热榜话题的数据抓取、分析与可视化python源码+...
Python 抓取百度贴吧里边的图片内容
这个是一个python脚本,加入了动态代理,主要做的就是实现百度百科自动抓取
简单易学的爬虫案例,百度贴吧爬虫案例源码