0

I'm writing a c# program to pull a .jpg image from an HTML document, but the name of the target image changes every so often. me being a very new programmer, i can not figure out how to achieve the desired result.

I am using webclient to download the html.

so i guess i have a few questions to ask here.

  1. how can i use a wildcard to assume the name and length of the image name?
  2. and how can i trim the HTML containers away from the target image in the document?
Andy G
  • 19,232
  • 5
  • 47
  • 69
DataHead
  • 19
  • 1
  • So you want every instance of the URL to the image/images? And then you want to go and download that image? – sealz Aug 28 '13 at 20:10
  • Does the target image appear in a particular place in the HTML? Can you identify it by the surrounding characters? Have you looked at the string class to see how you might use those methods to locate and extract particular substrings? – Jim Mischel Aug 28 '13 at 20:12
  • As Jim mentioned I have done somthing like that. Read the entire html in as a string>>read until you hit the start of a link>>read till you hit the image extension>>Webclient download. Continue moving through the file. If you show us what you have tried or what has you stuck we can help you. – sealz Aug 28 '13 at 20:14
  • it is identifiable by a path, so targeting the location of it in the html is not a problem. its the name of the image that is troublesome – DataHead Aug 28 '13 at 20:22

2 Answers2

2

In short, using the approach you've described: you can't. HTTP requires that each individual requested resource be accessed by its name, you cannot ask a HTTP server to return a set of resources whose names match a pattern (be it a wildcard expression or a regex).

If, however, you know the names exist between a particular range and follow a pattern then you could create a series of requests and handle 404 errors accordingly, like so:

String resource = "/images/aestheticallyAttractiveHumanFemalesWithoutClothing/img_{0}.jpg";
for(int i=1;i<100;i++) {

    String thisResource = String.Format(CultureInfo.InvariantCulture, resource, i);

    HttpWebRequest request = new (HttpWebRequest)WebRequest.Create(thisResource);
    HttpWebResponse response = request.GetResponse();
    if( response.Status == HttpStatus.OK ) {
        using(Stream rs = response.GetResponseStream())
        using(FileStream fs = new FileStream(Path.Combine("C:\\Temp\\IRSTaxReturns2011\\" + i.ToString() + ".jpg") {
            rs.CopyTo( fs );
        }
    }
}
Dai
  • 141,631
  • 28
  • 261
  • 374
2

You should scrape the webpage to get the image url then download the image. For the scraping check out:

https://github.com/jamietre/CsQuery

https://code.google.com/p/fizzler/

https://code.google.com/p/sharp-query/

Is there a jQuery-like CSS/HTML selector that can be used in C#?

These will allow to you select the element you care about based on attribute name, position in the document, or a combo of these identifiers and then get the src attribute.

  1. Download webpage html
  2. Parse html to get the url of the image
  3. Download the image

Edit: @Jacob Proffitt Cool stuff if your ok with XPath

http://htmlagilitypack.codeplex.com/

How to use HTML Agility pack

Community
  • 1
  • 1
Matt Urtnowski
  • 2,556
  • 1
  • 18
  • 36