I am writing a smallProgramWhen you encounter such a requirement:
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 ; // This 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 ; // Loading
Browser. navigate (URL );
While(Loading)
{
Application. doevents ();//The next loop is executed only after the loading is completed.
}
}
}
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 the next loop starts to execute.
}
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 ; // This 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 ; // Loading
Browser. navigate (URL );
While(Loading)
{
Application. doevents ();//The next loop is executed only after the loading is completed.
}
}
}
Int I = 0 ;
Void Browser_navigated ( Object Sender, webbrowsernavigatedeventargs E)
{
I ++ ;
If (I % 3 = 0 ) // Assume that the browser_navigated method is executed three times on each page. This means that all the content of the webpage is loaded)
{
Html = Browser. documenttext; // The obtained HTML.
Loading = False ; // After loading is complete, set this variable to false and the next loop starts to execute.
}
}
The above is just a small summary of my work. I have written some notes and hope to help others. I believe there are many ways to solve this problem. I hope you can give me some advice...