Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var extBooks = readeFromFile("Books.json");

            if (extBooks == null)
            {
                extBooks = new List <Book>();
            }
            var datas  = new List <Book>();
            var random = new Random(DateTime.Now.Millisecond);

            for (int i = 10883; i >= 8083; i--)
            {
                var  url  = string.Format("http://www.zxcs8.com/post/{0}", i);
                Book book = extBooks.Find(b => b.Url == url);
                if (book == null)
                {
                    book = getBook(url);
                }

                var urls = string.Format("http://www.zxcs8.com/content/plugins/cgz_xinqing/cgz_xinqing_action.php?action=mood&id={0}&typee=mood1&m={1}", i, random.NextDouble());
                var docs = HttpHelp.getDocument(urls);
                if (!docs.StartsWith("Error:"))
                {
                    book.Starts = docs;
                }

                datas.Add(book);

                if (datas.Count % 10 == 0)
                {
                    extBooks.AddRange(datas);
                    string jsw = JsonConvert.SerializeObject(extBooks);

                    writeToFile(jsw, "Books.json");
                    datas.Clear();
                    Console.WriteLine("Writed!");
                }

                Thread.Sleep(300);
            }

            string js = JsonConvert.SerializeObject(extBooks);

            writeToFile(js, "Books.json");
            Console.WriteLine("Ok!");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        private static Book getBook(string url)
        {
            var doc = HttpHelp.getDocument(url);

            if (doc.StartsWith("Error:"))
            {
                Thread.Sleep(200);
            }

            Regex reg = new Regex(@"<h1>(.|\n)*?</h1>");

            string[] taas = reg.Match(doc).Value.Replace("<h1>", "").Replace("</h1>", "").Split("作者:");

            reg = new Regex(@"<p\sclass=""date"">(.|\n)*?</p>");

            string kwp = reg.Match(doc).Value;

            reg = new Regex(@"<a(.|\n)*?</a>");

            var kwl = new List <string>();

            foreach (Match item in reg.Matches(kwp))
            {
                kwl.Add(Helper.GetTitleContent(item.Value, "a"));
            }
            if (kwl.Count > 0)
            {
                kwl.RemoveAt(0);
            }

            var keywords = string.Join(',', kwl);


            Book book = new Book()
            {
                Url = url, KeyWords = keywords
            };

            book.Title = taas[0];
            if (taas.Length > 1)
            {
                book.Author = taas[1];
            }
            return(book);
        }