用C#從人教社下載中小學電子教材

昨天看新聞,說人教社開放了人教版中小學教材電子版的春季教材(下載地址:http://bp.pep.com.cn/jc/ ),就想着給兒子全下載下來以備後用。不過人工下載真是麻煩枯燥,就爲了省事,就寫個爬蟲。本來打算用python,回頭想了下,很久沒用C#了,就用C#寫吧。html

具體思路和實現步驟以下python

1. 分析相關網頁的結構和鏈接跳轉來了解如何獲取到電子書的網頁地址。git

首先,涉及的頁面主要又兩頁,第一個頁面是分類目錄頁面,裏面按小學,中學這些分了大的類別,每一個大的類別下面又有學科這些小的類別,第二個是每一個學科下的各個年級的電子書下載詳情頁面。github

根據上述兩個頁面的狀況,我決定首先從第一個頁面來獲取到全部大類及各個大類下面每一個學科的網頁地址,再依次迭代上述各學科網頁的內容,從其內容獲取每一個電子書的地址,最後來多線程異步來下載每一個學科下的電子書。web

2. 要從html頁面獲取電子書地址,就必須用到兩個類庫,一個用來處理訪問網頁和網絡下載的網絡類,一個是用來分析html結構的類庫。這裏我選用了WebClient和HtmlAgilityPack。網絡

3. 根據第1步的思路,先分析分類目錄的頁面的html代碼結構狀況,用第二步選擇的類庫來實現獲取大分類目錄及其下各學科頁面網址,返回結果用Dictionary<string,List<string>>來存放,其中,key表示小學,初中,高中這些大的分類名稱,List<string>表示大分類下各學科的頁面地址。 具體實現代碼以下:多線程

            //獲取各學科各頁面地址
            public async Task<Dictionary<string, List<string>>> GetSubjectPageUrlsAsync()
            {
                var url = BASE_URL;
                Dictionary<string, List<string>> bookUrls = new Dictionary<string, List<string>>();

                var categoryXpath = "//*[@id=\"container\"]/div[@class=\"list_sjzl_jcdzs2020\"]";

                //獲取指定地址的html頁面內容
                WebClient webClient = new WebClient();
                var content = await webClient.DownloadStringTaskAsync(url);

                //加載html內容到HtmlDocument以便處理內容
                HtmlDocument htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(content);

                //獲取指定路徑的節點集合
                HtmlNodeCollection booksListEle = htmlDocument.DocumentNode.SelectNodes(categoryXpath);

                if (booksListEle != null)
                {
                    foreach (var item in booksListEle)
                    {
                        //獲取中學,小學等這些分類名稱
                        string title = string.Empty;
                        var titleNode = item.SelectSingleNode(".//div[@class=\"container_title_jcdzs2020\"]");
                        if (titleNode != null)
                        {
                            title = titleNode?.InnerText;
                        }

                        //獲取中學,小學等這些分類下的各學科頁面所在地址
                        HtmlNodeCollection urlsNodes = item.SelectNodes(".//a");
                        if (urlsNodes?.Count > 0)
                        {
                            var list = new List<string>();
                            foreach (HtmlNode urlItem in urlsNodes)
                            {
                                var fullUrl = url + urlItem.Attributes["href"].Value.Substring(2);
                                list.Add(fullUrl);
                            }

                            if (!string.IsNullOrEmpty(title) && list.Count > 0)
                            {
                                bookUrls.Add(title, list);
                            }
                        }
                    }
                }
                return bookUrls;
            }

 

4.  迭代第3步所示結果,從各個學科頁面內容中進行電子書地址提取。具體代碼以下:異步

            //獲取各學科頁面中的電子書地址
            private async Task<(string Subject, List<(string BookName, string BookUrl)> Books)> GetSubjectBooksAsync(string url)
            {
                const string contentRootXpath = "//*[@id=\"container\"]/div[@class=\"con_list_jcdzs2020\"]";

                //Get html content
                WebClient client = new WebClient();
                string webcontent = await client.DownloadStringTaskAsync(url);

                //load html string with HtmlDocument
                HtmlDocument htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(webcontent);

                HtmlNode rootNode = htmlDocument.DocumentNode.SelectSingleNode(contentRootXpath);

                //Get the subject.獲取學科名稱
                HtmlNode titleEle = rootNode.SelectSingleNode(".//div[@class=\"con_title_jcdzs2020\"]");
                string subject = string.Concat(titleEle?.InnerText.Where(c => !char.IsWhiteSpace(c)));

                //Get all books of the subject. 
                //獲取學科下全部書列表並開始下載
                HtmlNodeCollection bookNodes = rootNode.SelectNodes(".//li");
                List<(string BookName, string BookUrl)> books = new List<(string BookName, string BookUrl)>();
                if (bookNodes != null && bookNodes.Count>0)
                {
                    string bookName = null;
                    string bookUrl = null;

                    foreach (HtmlNode liItem in bookNodes)
                    {
                        bookName = FixFileName(string.Concat(liItem.ChildNodes["h6"].InnerText.Where(c => !char.IsWhiteSpace(c))));//get book's name
                        bookUrl = liItem.ChildNodes["div"].ChildNodes[3].Attributes["href"].Value;//get the url of ebook

                        books.Add((bookName, bookUrl));
                    }
                }
                return (subject,books);
            }

5. 用從第4步中的獲取的電子書地址開始下載電子書。具體代碼以下:async

//下載單個科目下的全部書籍
            private async Task DownloadBooksAsync(string dir, string baseUrl, (string Subject, List<(string BookName, string BookUrl)> Books) books,Action<string, string> callback)
            {
                //Create the subdirectory under the specified directory.
                //建立子目錄
                dir = Path.Combine(dir, books.Subject);
                dir = FixPath(dir);
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                //構建下載任務列表
                List<Task> downloadTasks = new List<Task>();
                int count = 0;
                foreach (var book in books.Books)
                {
                    WebClient wc = new WebClient();
                    Uri.TryCreate(baseUrl + book.BookUrl[2..], UriKind.Absolute, out Uri bookUri);
                    var path = Path.Combine(dir, @$"{book.BookName}.pdf");
                    var fi = new FileInfo(path);
                    if (!fi.Exists || fi.Length == 0)
                    {
                        var task = wc.DownloadFileTaskAsync(bookUri, path);
                        downloadTasks.Add(task);
                        count++;
                    }
                }

                //等待全部下載任務執行完後,執行回調函數
                await Task.WhenAll(downloadTasks).ContinueWith((task) => { callback(books.Subject ?? string.Empty, count.ToString()); });
            }

 

6. 到這裏,最核心幾個方法已經完成。下來就能夠根據本身的界面交互須要,來選擇相應的實現方式,例如圖形界面,控制檯或者網頁等,並來根據面向界面編寫具體的應用邏輯。爲了節省時間和簡單起見,我選擇了控制檯。其具體的代碼不在這裏敘述了,若有興趣,能夠從github下載完整代碼查看。具體github的地址爲:https://github.com/topstarai/PepBookDownloader函數

相關文章
相關標籤/搜索