C# extract img url from web content then download the img

static void Main(string[] args)
        {
            WebClientDemo();
            
            Console.ReadLine();
        }

         
        static void WebClientDemo()
        {
            webContent = File.ReadAllText("img2.txt");
            var urlsList = webContent.Split(new string[] { "\"", "" }, StringSplitOptions.None).ToList().Where(x => x.StartsWith("http")).Where(x => x.EndsWith("jpg") || x.EndsWith(".png") || x.EndsWith(".jpeg"));

            foreach (var ul in urlsList)
            {
                WebClientDownload(ul);
            }
        }

        static void WebClientDownload(string url)
        {
            try
            {
                using (WebClient wc = new WebClient())
                {               
                   
                    string[] urls = url.Split(new string[] { "/" }, StringSplitOptions.None);
                    string fileName = "Imgs2\\"+ urls[urls.Length - 1];
                    wc.DownloadFile(url, fileName);
                    ++num;
                    Console.WriteLine(url);
                }
            }
            catch
            {

            }           
        }

 

posted @ 2019-12-30 09:44  FredGrit  阅读(287)  评论(0编辑  收藏  举报