Regular Expression to get the SRC of images in C#

后端 未结 8 1510
情深已故
情深已故 2020-11-29 09:34

I\'m looking for a regular expression to isolate the src value of an img. (I know that this is not the best way to do this but this is what I have to do in this case)

8条回答
  •  攒了一身酷
    2020-11-29 09:48

    I tried what Francisco Noriega suggested, but it looks that the api to the HtmlAgilityPack has been altered. Here is how I solved it:

            List images = new List();
            WebClient client = new WebClient();
            string site = "http://www.mysite.com";
            var htmlText = client.DownloadString(site);
    
            var htmlDoc = new HtmlDocument()
                        {
                            OptionFixNestedTags = true,
                            OptionAutoCloseOnEnd = true
                        };
    
            htmlDoc.LoadHtml(htmlText);
    
            foreach (HtmlNode img in htmlDoc.DocumentNode.SelectNodes("//img"))
            {
                HtmlAttribute att = img.Attributes["src"];
                images.Add(att.Value);
            }
    

提交回复
热议问题