How would I download all kinds of file types from

2019-02-20 17:06发布

I have the following code in a new class:

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;

            }
        }
    }
}

The above code is part of my WebCrawler. This code will download only image files from a website.

For example, I have this site: http://web.archive.org/web/20131216195236/http://open-hardware-monitor.googlecode.com/svn/trunk/

Contained in the aforementioned site is a file named App. If I right click it and save as, then I see that it's a config file. If I click on the Hardware/ link, then I see many *.CS files.

How can I make and/or update my code so that it will download all kinds of file types rather than only downloading images?

1条回答
混吃等死
2楼-- · 2019-02-20 17:21

Right now the following line:

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

Is grabbing all image tags and processing them. You will need to find a way to find all anchor tags where the href extension equals .cs

It will be similar to the line above. I recommend reading up on xPath, since that appears to be what SelectNodes is using to find elements.

Hope this helps you get started!

查看更多
登录 后发表回答