[源码]打包下载算法与数据结构演示动画

很早的时候,学习数据结构的时候。收集了一下演示的动画。帮助理解。但是不全。今天在看KMP算法的时候。看到了福州大学的一个精品课程。。81个演示动画呢。。想打包下载收藏。话说福州大学这才是好样的。踏踏实实搞学术。

第一种方法就是手工了。。嘎嘎。你敢么。一个个下载。。。一个个改名。。

第二种就是用整站下载的软件了。。但是我看了一下swf的命名。我就知道下载下来意义不大。因为名字不好理解。

第三种就是自己写个程序吧。。

 

整体思路,首先访问课程页面,解析得到每一章的标题和内容,然后创立章节文件夹,得到每个动画对应的html页面,然后对html页面解析,提取swf地址。然后下载就行了。

比较疼的地方是那个页面用的是gb2312编码。而解析神器HtmlAgilityPack,不能指定编码。只能想办法绕过了。

      WebClient client = new WebClient();
            MemoryStream ms = new MemoryStream(client.DownloadData(url));
            HtmlDocument doc = new HtmlDocument();
            doc.Load(ms, Encoding.GetEncoding("gb2312"));

 

绕过方法就是先使用内置类得到内存流。然后从内存中加载。

然后呢。涉及的技术就是xpath了。参考着xpath的文档。搞定了不少。中间还有一个地方就是我没注意看。这个页面有两个文件是一样名字。。调试了几次才发现。。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using System.IO;
using System.Threading;
using System.Net;

namespace FzuSwf
{
    class Program
    {
        static void Main(string[] args)
        {
            DoWork();
        }

        //执行任务
        static void DoWork()
        {
            HtmlWeb web = new HtmlWeb();
            HtmlDocument doc = web.Load("http://ds.fzu.edu.cn/fine/resources/");
            HtmlNode divResource = doc.GetElementbyId("divResource");
            foreach (HtmlNode child in divResource.ChildNodes)
            {
                if (child.Name == "table")
                {
                    HtmlNode ptile = child.SelectSingleNode("tr[1]");

                    Directory.CreateDirectory(ptile.InnerText.Trim());
                    int i = 0;
                    HtmlNodeCollection pcontents = child.SelectNodes("tr[position()>1]");
                    Console.WriteLine(ptile.InnerText.Trim());
                    foreach (HtmlNode one in pcontents)
                    {
                        string link = one.SelectSingleNode("./td[1]/p[1]/a[@href]").Attributes["href"].Value;

                        link = @"http://ds.fzu.edu.cn/fine/resources/" + link;

                        string filename;

                        filename = one.InnerText.Trim();
                        if (one.InnerText.Trim() == "二叉树的顺序存储表示")
                        {
                            filename += i;
                            i++;
                        }
                        string swfLink = getSwfName(link);
                        Console.WriteLine("--" + filename + swfLink);
                        DownSwf(swfLink, ptile.InnerText.Trim() + @"/" + filename + ".swf");

                        Thread.Sleep(1000);

                    }
                }

            }
        }

        //下载指定的swf
        static void DownSwf(string url, string desname)
        {
            Uri u = new Uri(url);
            WebClient myWebClient = new WebClient();
            myWebClient.DownloadFileAsync(u, desname);
        }

        //获取指定页面的那个swf名称
        static string getSwfName(string url)
        {
            WebClient client = new WebClient();
            MemoryStream ms = new MemoryStream(client.DownloadData(url));
            HtmlDocument doc = new HtmlDocument();
            doc.Load(ms, Encoding.GetEncoding("gb2312"));
            HtmlNode hd = doc.DocumentNode;

            string str = hd.SelectSingleNode("//a[@href]").Attributes["href"].Value;

            return @"http://ds.fzu.edu.cn/fine/resources/" + str;
        }

    }
}

 

 

写完运行。看着如下这个界面。。这个我只是为了让我看到进度。同时后台也在下载。

运行完成后。文件夹和文件自动命名完成是这样的。。

 

下载地址:打包动画演示下载

我的博客:http://leaver.me

posted @ 2012-12-03 20:37  lazycoding  阅读(7339)  评论(21编辑  收藏  举报