如何使用C#下载html网页

时间:2019-03-29 22:04:21

标签: c#

我需要下载一些网页  像

../directory/page_1.html
../directory/page_2.html 
...
../directory/page_80.html

并将它们保存到文件夹中,我可以使用c#完成此简单任务吗?请提供帮助。任何建议

for(i=1;i<81;i++){
string url = "http://mywebsite.com/cats/page_" + convert.tostring(i)
+ ".html"
//
// code to download the html page 
//
}

2 个答案:

答案 0 :(得分:0)

new WebClient ().DownloadFile("http://abc.html", @"C:\downloadedFile_abc.html");

// Or you can get the file content without saving it
string htmlCode = new WebClient ().DownloadString("http://abc.html");

答案 1 :(得分:0)

for(i=1;i<81;i++){
string url = "http://mywebsite.com/cats/page_" + convert.tostring(i)
+ ".html"
using (WebClient client = new WebClient ())
{
    //you can download as a html file 
    client.DownloadFile(url, @"C:\" + i.ToString() + ".html");

    // Or you can get html source code as a string
    string htmlCode = client.DownloadString(url);
}
}

如果您想从html代码中获取一些数据(例如具有“ xx” className的表),则可以使用HTMLAGILITYPACK