我有一个网站的链接,如何从该网站下载所有文件

本文关键字:网站 下载 文件 有一个 链接 | 更新日期: 2023-09-27 18:35:26

例如:http://www.test.com我的程序正在挖掘爬行。所以我希望它每次都会下载所有文件。

例如:

using (WebClient Client = new WebClient ())
{
    Client.DownloadFile("http://www.abc.com/file/song/a.mpeg", "a.mpeg");
}

这将仅下载特定的 a.mpeg 文件。我想做这样的事情:

using (WebClient Client = new WebClient ())
{
    Client.DownloadFile(address, "*.*");
}

由于地址一直在变化,我想下载所有文件,而不是像 mpeg 或 jpg 或 avi 这样的特定文件......任何扩展。

做"."是正确的方法吗?

编辑**

这就是我今天下载图像的方式:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HtmlAgilityPack;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Net;
using System.Web;
using System.Threading;
using DannyGeneral;
using GatherLinks;
namespace GatherLinks
{
    class RetrieveWebContent
    {
        HtmlAgilityPack.HtmlDocument doc;
        string imgg;
        int images;
        public RetrieveWebContent()
        {
            images = 0;
        }
        public List<string> retrieveFiles(string address)
        {
        }
        public List<string> retrieveImages(string address)
        {
            System.Net.WebClient wc = new System.Net.WebClient();
            List<string> imgList = new List<string>();
            try
            {
                    doc = new HtmlAgilityPack.HtmlDocument();
                    doc.Load(wc.OpenRead(address));
                    string t = doc.DocumentNode.InnerText;
                    HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[@src]");
                    if (imgs == null) return new List<string>();
                    foreach (HtmlNode img in imgs)
                    {
                        if (img.Attributes["src"] == null)
                            continue;
                        HtmlAttribute src = img.Attributes["src"];
                        imgList.Add(src.Value);
                        if (src.Value.StartsWith("http") || src.Value.StartsWith("https") || src.Value.StartsWith("www"))
                        {
                            images++;
                            string[] arr = src.Value.Split('/');
                            imgg = arr[arr.Length - 1];
                            //imgg = Path.GetFileName(new Uri(src.Value).LocalPath);
                            //wc.DownloadFile(src.Value, @"d:'MyImages'" + imgg);
                            wc.DownloadFile(src.Value, "d:''MyImages''" + Guid.NewGuid() + ".jpg");
                        }
                    }
                return imgList;
            }
            catch
            {
                Logger.Write("There Was Problem Downloading The Image: " + imgg);
                return null;
            }
        }
    }
}

现在在代码的位置:

public List<string> retrieveFiles(string address)
        {
        }

我不想只下载jpg文件,而是下载任何类型的文件。如果链接是例如:http://tes.com''i.jpg为什么我需要解析网站而不是以某种方式保存?

我有一个网站的链接,如何从该网站下载所有文件

不,WebClient.DownloadFile 永远不会像爬虫一样运行。您需要下载该页面并在返回的页面 HTML 上使用 C# HtmlParser,枚举您感兴趣的资源并单独下载它们。