1

新しいクラスに次のコードがあります。

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> retrieveImages(string address)
        {
            try
            {
                doc = new HtmlAgilityPack.HtmlDocument();
                System.Net.WebClient wc = new System.Net.WebClient();
                List<string> imgList = new List<string>();
                doc.Load(wc.OpenRead(address));
                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];
                        wc.DownloadFile(src.Value, @"d:\MyImages\" + imgg);
                    }
                }

                return imgList;
            }
            catch
            {
                Logger.Write("There Was Problem Downloading The Image: " + imgg);
                return null;

            }
        }
    }
}

上記のコードは、私の WebCrawler の一部です。このコードは、Web サイトから画像ファイルのみをダウンロードします。

たとえば、次のサイトがあります: http://web.archive.org/web/20131216195236/http://open-hardware-monitor.googlecode.com/svn/trunk/

前述のサイトには、App. それを右クリックしてsave asすると、それが構成ファイルであることがわかります。リンクをクリックすると、Hardware/多くの *.CS ファイルが表示されます。

画像だけをダウンロードするのではなく、あらゆる種類のファイルをダウンロードするようにコードを作成および/または更新するにはどうすればよいですか?

4

1 に答える 1

4

現在、次の行:

HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[@src]");

すべての画像タグを取得して処理しています。href拡張子が等しいすべてのアンカータグを見つける方法を見つける必要があります.cs

上記の行と同様になります。SelectNodes要素を見つけるために使用しているように見えるので、xPath を読むことをお勧めします。

これがあなたが始めるのに役立つことを願っています!

于 2013-03-12T17:28:05.040 に答える