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
            {

            }           
        }

 

你可能感兴趣的:(C# extract img url from web content then download the img)