[源碼]打包下載算法與數據結構演示動畫


很早的時候,學習數據結構的時候。收集了一下演示的動畫。幫助理解。但是不全。今天在看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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM