I need to extract image urls from HTML File in C#

Posted in Help the coder! on Apr 26, 2009 at 18:56 IST (about 1 year ago). Subscribe to this post Bookmark and Share Email
Showing comments 1 to 5 of total 5 on page 1 of 1
Tagged C#, html, images, URL
Post reply
« Previous1Next »

mojhongg
Rank: 218

Can anyone help me by explaining how to extract image urls from HTML File in C#

Posted by mojhongg on Sunday, April 26, 2009, 6:56 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

thilak
Rank: 314

But try to search for a regular expression containing "img src= ..>"

maybe something like '#]src\s=\s*(["\'])(.*?)\1#im'

Check this article

Posted by thilak on Sunday, April 26, 2009, 8:54 pm
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

coolcode
Rank: 90

The HTML Agility Pack can do this - just use a query like //img and access the src - like so:

string html;
using (WebClient client = new WebClient()) {
    html = client.DownloadString("http://www.google.com");
}
HtmlDocument doc = new HtmlDocument();        
doc.LoadHtml(html);
foreach(HtmlNode img in doc.DocumentNode.SelectNodes("//img")) {
    Console.WriteLine(img.GetAttributeValue("src", null));
}
Posted by coolcode on Monday, April 27, 2009, 1:55 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

joshi007
Rank: 269

You have to parse the HTML and check the img tag use the following link it includes C# library for parsing HTML tags i faced your problem b4 and i used this library and working well with me Parsing HTML tags

Posted by joshi007 on Tuesday, April 28, 2009, 1:39 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment

manojme
Rank: 67
new Regex(@"]*src\s*=\s*((('(?[^']*)')|((""(?[^""]*)"")))[^>]*>")
    .Matches(htmlString).OfType
().Select(x=>x.Groups[1].Value).ToList();

Not tested.

Posted by manojme on Wednesday, April 29, 2009, 1:37 am
  • Currently 0.00/5

0 votes

Thank this userFlag this comment
Pages: « Previous1Next »

Post your comment (No registration required)

  Add my comment  

TechieDesi Community

Not signed in (Sign-in or Register)
Be a true TechieDesi!
Top 10 Users
Spread the word
Invite your friends
Fan stuff
Help us improve
Need Help
FAQ's
Search tips
Found a bug? Report!
Feeds and letters
Subscribe via RSS
Archives
Subscribe to newsletter
Unsubscribe e-mail
Miscellaneous
Privacy policy
Visit rootnerve
About us
About us
Support the development
Official Blog
Advertise with us
Careers
Copyright (c) 2008, TechieDesi.com. All rights reserved | About us | Do-Not-Disturb registry | Powered by rootnerve | Page rendered in 0.192 seconds