Recently, when I was writing a small program, I encountered the following requirements:
If you know a set of web page URLs and want to get the html of each web page, you actually want to use the WebBrowser in the loop statement to load each web page and then get their html,
To implement this function, think about it as a very simple task, but in actual operations there is a problem, because the loading of loop statements and WebBrowser is not synchronized, leading to the previous
The previous page has not been loaded, and the next cycle starts again .... the final result is that WebBrowser only obtains the html of the last page. to solve this problem, what we need to do is
Wait until the web page is loaded, and then execute the next loop to load the following web page ....., according to this idea, I wrote the following program, which is effective after testing.
Bool loading = true; // The variable indicates whether the webpage is being loaded.
String html = string. Empty;
WebBrowser browser = new WebBrowser ();
Public void GetHtml (string [] urls)
{
Browser. Navigated + = new WebBrowserNavigatedEventHandler (browser_Navigated );
Foreach (string url in urls)
{
Loading = true; // indicates loading
Browser. Navigate (url );
While (loading)
{
Application. DoEvents (); // wait until the current loading is completed before the next loop is executed.
}
}
}
Void browser_Navigated (object sender, WebBrowserNavigatedEventArgs e)
{
Html = browser. DocumentText; // The obtained html.
Loading = false; // after loading is complete, set this variable to false and execute the next loop immediately.
}
The above problem is solved, and the following problem occurs: sometimes when a page is loaded, browser_Navigated will be executed multiple times.
I checked the online information because the page contains <iframe> </iframe>. Each <iframe> triggers browser_Navigated,
Therefore, the above procedures can be improved as follows:
Bool loading = true; // The variable indicates whether the webpage is being loaded.
String html = string. Empty;
WebBrowser browser = new WebBrowser ();
Public void GetHtml (string [] urls)
{& Nbs