簡體   English   中英

我如何從網站下載各種文件類型?

[英]How would I download all kinds of file types from a website?

我在新類中有以下代碼:

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的一部分。 此代碼將僅從網站下載圖像文件。

例如,我有這個網站: http//web.archive.org/web/20131216195236/http : //open-hardware-monitor.googlecode.com/svn/trunk/

前面提到的站點中包含一個名為App的文件。 如果我右鍵單擊它並save as ,那么我看到它是一個配置文件。 如果我點擊Hardware/鏈接,那么我會看到很多* .CS文件。

如何制作和/或更新我的代碼,以便下載各種文件類型而不是僅下載圖像?

現在是以下行:

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

抓取所有圖像標簽並處理它們。 您將需要找到一種方法來查找href擴展名等於.cs所有錨標記

它將類似於上面的一行。 我建議閱讀xPath,因為這似乎是SelectNodes用於查找元素的內容。

希望這有助於您入門!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM