Пример #1
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.UTF8;
            string       fpath   = "D:\\result.txt";
            FileStream   fs      = new FileStream(fpath, FileMode.Create);
            StreamWriter swriter = new StreamWriter(fs, Encoding.UTF8);

            //GET : HTTPS://www.adayroi.com/
            TinTrinhLibrary.WebClient client = new TinTrinhLibrary.WebClient();                                                            //tạo kết nối
            for (int i = 1; i <= 2; i++)                                                                                                   //vòng lặp tăng số trang lên
            {
                string          html     = client.Get("https://www.adayroi.com/dong-ho-nam-m101?p=" + i, "https://www.adayroi.com/", "");  //đi tới trang ưeb
                MatchCollection listLink = Regex.Matches(html, "post-title\"><a href=\"/(.*?)title=.*?>");                                 //danh sách link từng sản phẩm
                foreach (Match link in listLink)                                                                                           //vòng lặp để vào từng sản phẩm
                {
                    string html1          = client.Get("https://www.adayroi.com/" + link.Groups[1].Value, "https://www.adayroi.com/", ""); //tạo đường link đến từng sản phẩm
                    Match  watch_name     = Regex.Match(html1, "item-title\">(.*?)</h1>");                                                 //lấy tên sản phẩm
                    Match  watch_category = Regex.Match(html1, "text-blue\".*?>(.*?)</a>");                                                //lấy danh mục sản phẩm
                    Match  watch_price    = Regex.Match(html1, "item-price.*?>(.*?)<span", RegexOptions.Singleline);                       //lấy giá sản phẩm
                    Match  watch_desc     = Regex.Match(html1, "itemprop=\"description\">.*?<p>(.*?)</p>", RegexOptions.Singleline);       //lấy mô tả về sản phẩm
                    swriter.WriteLine("Tên đồng hồ: " + watch_name.Groups[1].Value);                                                       //in ra
                    swriter.WriteLine("Danh muc: " + watch_category.Groups[1].Value);
                    swriter.WriteLine("Giá: " + watch_price.Groups[1].Value.Trim());
                    swriter.WriteLine("Mô tả: " + watch_desc.Groups[1].Value);
                    swriter.WriteLine("\n====================================\n");
                }
            }
            swriter.Flush();
            fs.Close();
        }
Пример #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Start...");
            //GET : HTTPS://tiki.vn
            TinTrinhLibrary.WebClient client = new TinTrinhLibrary.WebClient();
            int          stt    = 0;
            StreamWriter myFile = new StreamWriter(@"D:\Top100BookTiki.txt");

            //Vong lap so page cua top 100
            for (int i = 1; i <= 4; i++)
            {
                string link = "https://tiki.vn/bestsellers/sach-truyen-tieng-viet/c316?p=" + i;
                string html = client.Get(link, "https://tiki.vn", "");

                //Regex data
                MatchCollection author      = Regex.Matches(html, "data-brand=\"(.*?)\" data-category");
                MatchCollection name        = Regex.Matches(html, "data-title=\"(.*?)\" data-brand");
                MatchCollection price       = Regex.Matches(html, "data-price=\"(.*?)\" data-title");
                MatchCollection description = Regex.Matches(html, "description\">(.*?)<a");

                // 1 page bao gom 25 sp
                for (int j = 0; j < 25; j++)
                {
                    stt++;
                    Console.WriteLine("STT : {0}", stt);
                    Console.WriteLine("Ten Sach : {0}", name[j].Groups[1].Value);
                    Console.WriteLine("Ten Tac Gia : {0}", author[j].Groups[1].Value);
                    Console.WriteLine("Gia Sach : {0}", price[j].Groups[1].Value, " VND");
                    Console.WriteLine("  ");
                    //Output to log
                    myFile.WriteLine("STT : {0}", stt);
                    myFile.WriteLine("Ten Sach : {0}", name[j].Groups[1].Value);
                    myFile.WriteLine("Ten Tac Gia : {0}", author[j].Groups[1].Value);
                    myFile.WriteLine("Gia Sach : {0}", price[j].Groups[1].Value, " VND");
                    myFile.WriteLine("  ");
                }
                ;
            }
            myFile.Close();
            Console.Read();
        }