我有一个网站链接如何从网站上下载所有文件?

时间:2013-10-14 09:43:38

标签: c# winforms

例如: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为什么我需要解析网站而不是以某种方式保存?

1 个答案:

答案 0 :(得分:3)

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

相关问题