簡體   English   中英

如何從網站下載圖像到我的硬盤?

[英]How can I download the images from the site to my hard disk?

我正在嘗試下載圖像,而不是僅提取每個圖像的時間和日期。 該代碼有效,但僅適用於時間和日期。

using System;
using System.Linq;
using System.IO;
using System.Xml;
using System.Net;
using HtmlAgilityPack;
                    
public class Program
{
    public static void Main()
    {
        var wc = new WebClient();
        wc.BaseAddress = "https://something.com";
        HtmlDocument doc = new HtmlDocument();
        
        var temp = wc.DownloadData("/en");
        doc.Load(new MemoryStream(temp));       
        
        var secTokenScript = doc.DocumentNode.Descendants()
            .Where(e =>
                   String.Compare(e.Name, "script", true) == 0 &&
                   String.Compare(e.ParentNode.Name, "div", true) == 0 &&
                   e.InnerText.Length > 0 &&
                   e.InnerText.Trim().StartsWith("var region")
                  ).FirstOrDefault().InnerText;
        var securityToken = secTokenScript;
        securityToken = securityToken.Substring(0, securityToken.IndexOf("arrayImageTimes.push"));  
        securityToken = secTokenScript.Substring(securityToken.Length).Replace("arrayImageTimes.push('", "").Replace("')", "");
        var dates = securityToken.Trim().Split(new string[] { ";"}, StringSplitOptions.RemoveEmptyEntries);
        var scriptDates = dates.Select(x => new ScriptDate { DateString = x });
        foreach(var date in scriptDates) 
        {
            Console.WriteLine("Date String: '" + date.DateString + "'\tYear: '" + date.Year + "'\t Month: '" + date.Month + "'\t Day: '" + date.Day + "'\t Hours: '" + date.Hours + "'\t Minutes: '" + date.Minutes + "'");
        }
        
    }
    
    
    public class ScriptDate
    {
        public string DateString {get;set;}
        public int Year 
        {
            get
            {
                return Convert.ToInt32(this.DateString.Substring(0, 4));
            }
        }
        public int Month
        {
            get
            {
                return Convert.ToInt32(this.DateString.Substring(4, 2));
            }
        }
        public int Day
        {
            get
            {
                return Convert.ToInt32(this.DateString.Substring(6, 2));
            }
        }
        public int Hours
        {
            get
            {
                return Convert.ToInt32(this.DateString.Substring(8, 2));
            }
        }
        public int Minutes
        {
            get
            {
                return Convert.ToInt32(this.DateString.Substring(10, 2));
            }
        }                       
    }
}

但是我怎樣才能使用相同的代碼來下載和保存圖像呢?

試過這個但得到異常:

private void Download()
        {
            using (WebClient client = new WebClient()) // WebClient class inherits IDisposable
            {
                client.DownloadFile("https://something.com", @"C:\temp\localfile.html");
            }
        }

System.Net.WebException:“遠程服務器返回錯誤:(500)內部服務器錯誤。”

我可以獲取日期和時間,但無法獲取提取圖像鏈接的來源。

其中一張圖片的鏈接示例應該如何構建,如下所示:

https://something.com/image?type=infraPolair&region=tu&timestamp=202012150230

但我想從頁面中自動提取所有圖像的日期和時間,然后自動構建鏈接,然后下載圖像。

第一個代碼我可以獲得每個圖像的日期和時間,但我無法下載頁面的源代碼,因此我無法提取和構建圖像的鏈接。

這就是為什么我想以某種方式使用第一個代碼來構建圖像鏈接然后下載圖像。

不完全確定您想通過日期時間實現什么,但使用 HAP 您可以執行以下操作:


HtmlElementCollection elements = doc.DocumentNode.SelectNodes("//img");
foreach (HtmlElement imageElement in elements)
{
   var imageSrc = imageElement.Attributes["src"].Value
   Download(imageSrc);
}

…………


private void Download(src)
        {
            using (WebClient client = new WebClient()) // WebClient class inherits IDisposable
            {
                client.DownloadFile(src, @"C:\temp\" + uniqueNameForFile + ".jpg");
            }
        }

這不是一個完美的答案,但應該讓你繼續。 Src 可能是相對的,因此您可能必須添加基地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM