欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-11 16:28:32
static void Main(string[] args) { WebClientDemo(); Console.ReadLine(); } static void WebClientDemo() { webContent = File.ReadAllText("img2.txt"); var ... ......
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
            {

            }           
        }