2014-02-08 30 views
-1

代碼:如何下載所有htmls並將它們保存爲一個循環?

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給我所以變量我會像藍色的一部分,而不是一個變量。

回答

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); 
} 
相關問題