C# 如何从网站下载各种类型的文件?
我在一个新类中有以下代码:C# 如何从网站下载各种类型的文件?,c#,download,web-crawler,C#,Download,Web Crawler,我在一个新类中有以下代码: 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
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的一部分。此代码将仅从网站下载图像文件
例如,我有一个网站:
上述站点中包含一个名为App的文件。如果我右键单击它并另存为,那么我会看到它是一个配置文件。如果单击硬件/链接,则会看到许多*.CS文件
我如何制作和/或更新代码,使其能够下载所有类型的文件,而不仅仅是下载图像?现在请看下面一行:
HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[@src]");
正在抓取所有图像标签并对其进行处理。您需要找到一种方法来查找href扩展名等于.cs的所有锚定标记
它将类似于上面的一行。我建议阅读xPath,因为SelectNodes似乎就是用它来查找元素的
希望这能帮助你开始 现在请看下面一行:
HtmlNodeCollection imgs = doc.DocumentNode.SelectNodes("//img[@src]");
正在抓取所有图像标签并对其进行处理。您需要找到一种方法来查找href扩展名等于.cs的所有锚定标记
它将类似于上面的一行。我建议阅读xPath,因为SelectNodes似乎就是用它来查找元素的
希望这能帮助你开始