【问题标题】:How can I download the images from the site to my hard disk?如何从网站下载图像到我的硬盘?
【发布时间】:2020-12-15 01:59:54
【问题描述】:

我正在尝试下载图像,而不是仅提取每张图像的时间和日期。 该代码有效,但仅适用于时间和日期。

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

但我想从页面中自动提取所有图像的日期和时间,然后自动构建链接,然后下载图像。

第一个代码我可以获取每张图片的日期和时间,但我无法下载页面的源代码,因此我无法提取和构建图片的链接。

这就是为什么我想以某种方式使用第一个代码来构建图像链接,然后下载图像。

【问题讨论】:

  • 你应该尝试在 foreach 循环中下载。您已经拥有所需的所有数据。尝试将下载文件类型更改为 jpeg 而不是 html

标签: c# html-agility-pack


【解决方案1】:

不完全确定您想通过日期时间实现什么,但使用 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 可能是相对的,因此您可能必须添加基地址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 2018-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多