예제 #1
0
        /// <summary>
        /// 读取章节列表.
        /// </summary>
        /// <param name="bookUrl"></param>
        /// <returns></returns>
        public List <Chapter> ReadChapterList(string bookUrl)
        {
            List <Chapter> resultList = new List <Chapter>();



            WebClient client = new WebClient();

            client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
            Stream s = client.OpenRead(bookUrl);



            using (StreamReader sr = new StreamReader(s, Encoding.UTF8))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.IndexOf("class=\"subtitle\"") >= 0)
                    {
                        int urlBeginIndex = line.IndexOf("href=");
                        if (urlBeginIndex <= 0)
                        {
                            continue;
                        }


                        urlBeginIndex += 6;

                        int urlEndIndex = line.IndexOf("\"", urlBeginIndex);
                        if (urlEndIndex <= 0)
                        {
                            continue;
                        }



                        int nameBeginIndex = line.IndexOf(">", urlEndIndex);
                        nameBeginIndex++;

                        int nameEndIndex = line.IndexOf("<", nameBeginIndex);



                        Chapter chapter = new Chapter()
                        {
                            // Url.
                            ChapterUrl = line.Substring(urlBeginIndex, urlEndIndex - urlBeginIndex),

                            // 名称.
                            ChapterName = line.Substring(nameBeginIndex, nameEndIndex - nameBeginIndex),
                        };

                        chapter.FormatChapterCode();


                        resultList.Add(chapter);
                    }
                }
            }



            return(resultList);
        }