简体   繁体   中英

How do I download pictures from a webpage based on their LINKS on this page?

This is what I have so far but I'm running into an issue.

The page https://xxxxxxxx.zendesk.com/tickets/33126 contains a link to a .jpg image. I want to download this image. The page could have several images so I need to scan the page to find all the .jpg, .gif, etc.

I'm having an issue with my code at the end. I'll explain there.

    public static void GetTicketAttachments(string url)
    {   
        GetImages("https://xxxxxxxx.zendesk.com/tickets/33126");   
    }

static void GetImages(string url)
    {
        string responseString;
        HttpWebRequest initialRequest = (HttpWebRequest)WebRequest.Create(url);
        using (HttpWebResponse initialResponse = (HttpWebResponse)initialRequest.GetResponse())
        {
            using (StreamReader reader = new StreamReader(initialResponse.GetResponseStream()))
            {
                responseString = reader.ReadToEnd();
            }

            List<string> imageset = new List<string>();
            Regex regex = new Regex(@"f=""[^""]*jpg|bmp|tif|gif|png", RegexOptions.IgnoreCase);
            foreach (Match m in regex.Matches(responseString))
            {
                if (!imageset.Contains(m.Value))
                    imageset.Add(m.Value);
            }
            for (int i = 0; i < imageset.Count; i++)
                imageset[i] = imageset[i].Remove(0, 3);
            totalFiles = imageset.Count;
            currentFiles = totalFiles;

            foreach (string f in imageset)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(DownloadImage), f);
            }
        }
    }

The issue happens here. The object "path" is always null for some reason. Therefore I cannot download the picture.

static void DownloadImage(object path)
    {
        currentFiles--;
        path = Path.GetFileName(path.ToString());
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(path.ToString());
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            Image image = Image.FromStream(response.GetResponseStream());
            image.Save(@"C:\" + Path.GetFileName(path.ToString()));
        }
    }

Anyone know what could be the issue? The "picture count" is indeed counting 1 (one link TO a picture on the page).

Don't try and parse the document yourself. Take a look at the HTML Agility Pack ( http://htmlagilitypack.codeplex.com/ ) to extract meaningful information from HTML documents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM