Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#控制台应用程序中仅从整个html读取文件?_C#_Asp.net_Visual Studio_Console Application - Fatal编程技术网

如何在c#控制台应用程序中仅从整个html读取文件?

如何在c#控制台应用程序中仅从整个html读取文件?,c#,asp.net,visual-studio,console-application,C#,Asp.net,Visual Studio,Console Application,我需要从URL获取每个文件,这样我就可以对它们进行迭代 其想法是使用ImageMagick调整每个图像的大小,但首先我需要能够获取文件并对其进行迭代 这是我到目前为止所做的代码 using System; using System.Net; using System.IO; using System.Text.RegularExpressions; namespace Example { public class MyExample { public st

我需要从URL获取每个文件,这样我就可以对它们进行迭代

其想法是使用ImageMagick调整每个图像的大小,但首先我需要能够获取文件并对其进行迭代

这是我到目前为止所做的代码

using System;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;



namespace Example
{
    public class MyExample
    {

        public static void Main(String[] args)
        {
            string url = "https://www.paz.cl/imagenes_cotizador/BannerPrincipal/";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string html = reader.ReadToEnd();
                    Console.WriteLine(html);

                }
            }

            Console.ReadLine();
        }
    }
}
返回URL的整个html。但是,我只需要这些文件(所有图像),这样我就可以像预期的那样使用它们

你知道如何做到这一点吗?

你可以使用

比如说

var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);

var htmlNodes = htmlDoc.DocumentNode.SelectNodes("//a");

foreach (var node in htmlNodes)
{   
    Console.WriteLine(node.Attributes["href"].Value);
}
您可以使用加载和解析html页面。然后你可以提取所有你需要的信息

// TODO add a reference to NuGet package AngleSharp
private static async Task Main(string[] args)
{
    var config = Configuration.Default.WithDefaultLoader();
    var address = "https://www.paz.cl/imagenes_cotizador/BannerPrincipal";
    var context = BrowsingContext.New(config);
    var document = await context.OpenAsync(address);
    var images = document.Images.Select(img=>img.Source);

}

AngleSharp实现了w3c标准,因此它在现实世界的网页上比HTMLAgilityPack工作得更好。

我查看了该页面,它是一个目录/文件列表。您可以使用提取该页面正文中指向图像的所有链接

我可以想到一个模式:
HREF=“([^”]+\(jpg|png))

构建regex对象,迭代匹配项,并下载每个图像:

var regex = new System.Text.RegularExpressions.Regex("HREF=\"([^\"]+\\.(jpg|png))");
var matches = regex.Matches(html); // this is your html string
foreach(var match in matches) {
   var imagePath = match.ToString().Substring("HREF=\"".Length);
   Console.WriteLine(imagePath);
}

现在,连接基本url
https://www.paz.cl
使用上面获得的图像相对路径,向该url发出另一个请求,以下载图像并按您的意愿进行处理。

此操作的可能副本运行良好,现在的问题是如何在该url中创建目录,然后将每个文件的副本保存到itAh中,我认为这不太可能。该目录位于远程服务器上。如果您具有该服务器的FTP凭据,则可以使用某些FTP库,以编程方式登录,并上载生成的图像。