I need to 开发者_高级运维launch IE, browse to many different sites and save the pages that I browse to. Can .net work with IE like this or is a script a better approach?
If you just want to save the page do something like this. Here you get that html, not a screen shot of the page.
string url = "http://google.com";
string strResult = "";
WebResponse objResponse;
WebRequest objRequest = System.Net.HttpWebRequest.Create(url);
objResponse = objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
// Display results to a webpage
//Response.Write(strResult);
Console.WriteLine(strResult);
Console.ReadKey();
If you need a picture of the page use something like autoit or watin.
I would suggest using a WebBrowser Class for this. You can load the site and then you can use its event handlers to save the webpage.
The accepted answer is too complicated. If you really just need to download the source html, use this function:
function Download-Page([string]$url) {
$w = New-Object net.webclient
$w.DownloadString($url)
}
Then you can save the content like this:
Download-Page http://www.google.com | Set-Content d:\google.html
(this will work with localhost urls as well)
精彩评论