下载网易公开课 视频 代码

2015-01-28: 百度云共享算法导论视频地址:

http://pan.baidu.com/s/1hqeRyDY

如下代码可以解析 网易公开课算法导论的视频下载连接,主要是因为最近电脑客户端不能下载的问题。

遗留问题:有广告时会解析失败,失败概率 极低 但未研究了。 有空了再看下。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.httpclient.HttpException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class FetchURL {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws HttpException 
	 */
	public static void main(String[] args) throws HttpException, IOException {
		//use jsoup
		String url ="http://v.163.com/special/opencourse/[email protected]";
		Document doc = Jsoup.connect(url).get();
		Elements tbody = doc.select("tbody");
		Element tbodyf1 = tbody.first();
		
		Elements trs = tbodyf1.getElementsByTag("tr");
		Map<String,String> name2url = new HashMap<String,String>();
		
		for (int i=1;i<trs.size();i++)
		{
			Element e = trs.get(i);
			Element row = e.getElementsByTag("td").get(0).getElementsByTag("a").get(0);
			String t = row.attr("href");
			String name = i+"."+row.text();
			name2url.put(name, t);
			//第一部分
			dealOnePage(t, name+".mp4");
		}
		
		//可能会有的第二个list
		Elements list2 = doc.select("#list2");
		if (list2.size() != 0)
		{
			Elements list2trs  = list2.get(0).select("tbody").get(0).select("tr");
			for (int i=1;i<list2trs.size();i++)
			{
				Element e = list2trs.get(i);
				Element row = e.getElementsByTag("td").get(0).getElementsByTag("a").get(0);
				String t = row.attr("href");
				String name = i+"."+row.text();
				name2url.put(name, t);
				//第2部分
				dealOnePage(t, name+".mp4");
			}
		}
		
	}

	//对前面在项目页面获取的url进行处理 获取最终的视频 下载地址 并下载
	private static void dealOnePage(String url,String filename) throws IOException
	{
		System.out.println("Start deal:" + url);
		Document doc = Jsoup.connect(url).get();
		Elements els = doc.select("[href*=mov.bn.netease.com]");
		if (els.size() == 0) {
			System.out.println("Error. " + url);
			return;
			//这里有广告的时候有问题? TODO
//			try {
//				Thread.sleep(1000 * 30);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//			dealOnePage(url, filename);
		}
		String videopath = els.get(0).attr("href");
		System.out.println(filename +"->" +videopath);
		//打开这段代码开始下载
		//downfile(videopath,"t:\\",filename);
	}
	
	//下载单个文件 这里可以优化为多线程 但是意义不大 取决于网络状况;未考虑校验情况和下载失败情况
	private static void downfile(String url, String destPath, String fileName)
	{
		System.out.println("Start downfile:" + fileName);
		URL urlfile = null;
        HttpURLConnection httpUrl = null;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        File f = new File(destPath + File.separator + fileName);
        try
        {
            urlfile = new URL(url);
            httpUrl = (HttpURLConnection)urlfile.openConnection();
            httpUrl.connect();
            bis = new BufferedInputStream(httpUrl.getInputStream());
            bos = new BufferedOutputStream(new FileOutputStream(f));
            int len = 0;
            int total = 0;
            byte[] b = new byte[4096];
            while ((len = bis.read(b)) != -1)
            {
                bos.write(b, 0, len);
                total+=len;
                System.out.println("read:" + total/1024 + "KB");
            }
            bos.flush();
            bis.close();
            httpUrl.disconnect();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                bis.close();
                bos.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
	}
	
}

Start deal:http://v.163.com/movie/2010/12/G/F/M6UTT5U0I_M6V2T1JGF.html
1.课程简介及算法分析.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/11/28/SAB7OBN04_sd.mp4
Start deal:http://v.163.com/movie/2010/12/2/E/M6UTT5U0I_M6V2T4T2E.html
2.渐近符号、递归及解法.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/11/28/SAB7OAP87_sd.mp4
Start deal:http://v.163.com/movie/2010/12/8/U/M6UTT5U0I_M6V2T998U.html
3.分治法(1).mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF2U845_sd.mp4
Start deal:http://v.163.com/movie/2010/12/S/4/M6UTT5U0I_M6V2T7IS4.html
4.快排及随机化算法.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/11/28/SAB7OEJT1_sd.mp4
Start deal:http://v.163.com/movie/2010/12/7/V/M6UTT5U0I_M6V2TBK7V.html
5.线性时间排序.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/11/28/SAB7OIQLM_sd.mp4
Start deal:http://v.163.com/movie/2010/12/J/J/M6UTT5U0I_M756SE0JJ.html
Error. http://v.163.com/movie/2010/12/J/J/M6UTT5U0I_M756SE0JJ.html
Start deal:http://v.163.com/movie/2010/12/R/E/M6UTT5U0I_M6V2TG4RE.html
7.哈希表.mp4->http://mov.bn.netease.com/mobilev/2012/2/J/O/S7OT7V5JO.mp4
Start deal:http://v.163.com/movie/2010/12/3/A/M6UTT5U0I_M6V2TGI3A.html
8.全域哈希和完全哈希.mp4->http://mov.bn.netease.com/mobilev/2012/3/2/2/S7S43NP22.mp4
Start deal:http://v.163.com/movie/2010/12/6/U/M6UTT5U0I_M6V2TGB6U.html
9.二叉搜索树.mp4->http://mov.bn.netease.com/mobilev/2012/3/0/G/S7S45P40G.mp4
Start deal:http://v.163.com/movie/2010/12/9/J/M6UTT5U0I_M6V2TJ49J.html
10.平衡搜索树.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF3GJMI_sd.mp4
Start deal:http://v.163.com/movie/2010/12/G/F/M6UTT5U0I_M6V2T1JGF.html
1.课程简介及算法分析.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/11/28/SAB7OBN04_sd.mp4
Start deal:http://v.163.com/movie/2010/12/2/E/M6UTT5U0I_M6V2T4T2E.html
2.渐近符号、递归及解法.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/11/28/SAB7OAP87_sd.mp4
Start deal:http://v.163.com/movie/2010/12/8/U/M6UTT5U0I_M6V2T998U.html
3.分治法(1).mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF2U845_sd.mp4
Start deal:http://v.163.com/movie/2010/12/S/4/M6UTT5U0I_M6V2T7IS4.html
4.快排及随机化算法.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/11/28/SAB7OEJT1_sd.mp4
Start deal:http://v.163.com/movie/2010/12/7/V/M6UTT5U0I_M6V2TBK7V.html
5.线性时间排序.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/11/28/SAB7OIQLM_sd.mp4
Start deal:http://v.163.com/movie/2010/12/J/J/M6UTT5U0I_M756SE0JJ.html
Error. http://v.163.com/movie/2010/12/J/J/M6UTT5U0I_M756SE0JJ.html
Start deal:http://v.163.com/movie/2010/12/R/E/M6UTT5U0I_M6V2TG4RE.html
7.哈希表.mp4->http://mov.bn.netease.com/mobilev/2012/2/J/O/S7OT7V5JO.mp4
Start deal:http://v.163.com/movie/2010/12/3/A/M6UTT5U0I_M6V2TGI3A.html
8.全域哈希和完全哈希.mp4->http://mov.bn.netease.com/mobilev/2012/3/2/2/S7S43NP22.mp4
Start deal:http://v.163.com/movie/2010/12/6/U/M6UTT5U0I_M6V2TGB6U.html
9.二叉搜索树.mp4->http://mov.bn.netease.com/mobilev/2012/3/0/G/S7S45P40G.mp4
Start deal:http://v.163.com/movie/2010/12/9/J/M6UTT5U0I_M6V2TJ49J.html
10.平衡搜索树.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF3GJMI_sd.mp4
Start deal:http://v.163.com/movie/2010/12/G/0/M6UTT5U0I_M6V2TSIG0.html
11.扩充的数据结构、动态有序统计和区间树.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF3G4E4_sd.mp4
Start deal:http://v.163.com/movie/2010/12/7/S/M6UTT5U0I_M6V2TTJ7S.html
12.跳跃表.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF3HDGL_sd.mp4
Start deal:http://v.163.com/movie/2010/12/A/L/M6UTT5U0I_M6V2U1OAL.html
13.平摊分析,表的扩增,势能方法.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF3LDLT_sd.mp4
Start deal:http://v.163.com/movie/2010/12/I/2/M6UTT5U0I_M6V2U1BI2.html
14.竞争性分析,自组织表.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF3LGQH_sd.mp4
Start deal:http://v.163.com/movie/2010/12/L/4/M6UTT5U0I_M6V2U1HL4.html
15.动态规划,最长公共子序列.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF46AAP_sd.mp4
Start deal:http://v.163.com/movie/2010/12/1/S/M6UTT5U0I_M6V2U3R1S.html
16.贪婪算法,最小生成树.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF48R0R_sd.mp4
Start deal:http://v.163.com/movie/2010/12/E/P/M6UTT5U0I_M6V2U4BEP.html
17.最短路径算法:Dijkstra算法,广度优先搜索.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF4C87R_sd.mp4
Start deal:http://v.163.com/movie/2010/12/4/O/M6UTT5U0I_M6V2U754O.html
18.最短路径算法:Bellman和差分约束系统.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF4D3E2_sd.mp4
Start deal:http://v.163.com/movie/2010/12/8/H/M6UTT5U0I_M6V2UDN8H.html
19.最短路径算法:点的最短路径.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF4F27E_sd.mp4
Start deal:http://v.163.com/movie/2010/12/V/E/M6UTT5U0I_M6V2UDUVE.html
20.高级课题 并行算法(一).mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF5100E_sd.mp4
Start deal:http://v.163.com/movie/2010/12/0/B/M6UTT5U0I_M6V2UGJ0B.html
21.高级课题 并行算法(二).mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF51AR6_sd.mp4
Start deal:http://v.163.com/movie/2010/12/L/L/M6UTT5U0I_M6V2UIKLL.html
22.高级课题 缓存参数无关算法.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF5EFF2_sd.mp4
Start deal:http://v.163.com/movie/2010/12/E/1/M6UTT5U0I_M6V2UI5E1.html
23.缓存无关算法2.mp4->http://mov.bn.netease.com/open-movie/nos/mp4/2014/12/01/SABF4VNST_sd.mp4


你可能感兴趣的:(算法导论,网易公开课)