我如何下载所有的HTML并保存它们一个循环?

时间:2014-02-08 06:17:19

标签: c# winforms

代码:

client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=1&continent=europa#", localFilename + "\\Sat24_Rain_Europe.html");
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=2&continent=europa#", localFilename + "\\Sat24_Wind_Europe.html");
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=3&continent=europa#", localFilename + "\\Sat24_Lightnings_europe.html");
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=0&continent=europa#", localFilename + "\\Sat24_Temperature_Europe.html");
client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=4&continent=europa#", localFilename + "\\Sat24_Cloudtypes_Europe.html");

但是每次都要下载一个循环来运行htmls,如:

for (int i = 0; i < 4; i++)
                        {
                          client.DownloadFile("http://www.sat24.com/foreloop.aspx?type=i&continent=europa#", localFilename + "\\Sat24_Cloudtypes_Europe.html");
                        }

取而代之的是0 1 2 3 4 ...在循环中使用(i)。

但如果我改变了client.DownloadFile(“http://www.sat24.com/foreloop.aspx?type=4&continent=europa#”,localFilename +“\ Sat24_Cloudtypes_Europe.html”);

数字4到i所以变量i将是蓝色,就像网址的一部分而不是变量。

1 个答案:

答案 0 :(得分:2)

像这样:

var files = new Dictionary<int, string> {
   { 0, "Sat24_Temperature_Europe.html" },
   { 1, "Sat24_Rain_Europe.html" },
   { 2, "Sat24_Wind_Europe.html" },
   { 3, "Sat24_Lightnings_europe.html" },
   { 4, "Sat24_Cloudtypes_Europe.html" }
};

const string urlFormat = 
                  "http://www.sat24.com/foreloop.aspx?type={0}&continent=europa#";
foreach (var kv in files)
{
    string url = string.Format(urlFormat, kv.Key);
    client.DownloadFile(url, localFilename + "\\" + kv.Value);
}