C # Web browser is used in multiple threads to collect dynamic web pages. crawlers use C # webbrowser and application. doevents () to collect dynamic web pages.

Source: Internet
Author: User

Today, in the garden, we can see that the school School's ass uses C # webbrowser and application. doevents () to collect dynamic web pages.

In fact, I used a similar method to capture the web pages to be logged on, and dynamic page adding pages.

What I want to talk about today is how to implement multithreading and use webbortracing to collect pages.

Among them, I used a weifenluo. winformsui. Docking. dll, which is an open source component.

: Https://github.com/dockpanelsuite/dockpanelsuite/downloads

Some predecessors in the garden have made detailed usage of this component. I will not explain it more here.

Http://www.cnblogs.com/wuhuacong/archive/2009/07/09/1520082.html

Add a weifenluo control to the mainform, and set the ismdicontainer attribute of the mainform to true.

Add the addwebform method to dynamically Add sub-forms.

public void addWebForm(string s){    if (this.InvokeRequired)    {        this.BeginInvoke(new OneStringParmenters(addWebForm), s);    }    else    {        ChildForm f2 = new ChildForm();        f2.Text = s;        webForm.Add(s, f2);        f2.Show(dockPanel1);    }}

Add a webborwser control to the childform.

And add the method to open the page

Public Delegate void onestringparmenters (string Str); // one string parameter delegates public void navigate (string URL) {If (this. invokerequired) {This. begininvoke (New onestringparmenters (navigate), URL);} else {webbrowser1.navigate (URL );}}

And how to read html

Private string strhtmlleng = ""; private delegate void noparameters (); // No parameter delegate; Public String strhtmlleng {get {If (this. invokerequired) {iasyncresult IAR = This. begininvoke (New noparameters (gethtmlleng); While (! IAR. iscompleted) {system. threading. thread. sleep (0) ;}} else {gethtmlleng ();} return strhtmlleng ;}} private void gethtmlleng () {strhtmlleng = webbrowser1.documenttext ;}

Add a multithreading method to simulate multithreading to open a page and read html

Public static void openwebpage (Object strurl) {datetime dt = datetime. now; string u = strurl. tostring (); iasyncresult IAR = program. form. begininvoke (New onestringparmenters (program. form. addwebform), U); While (! IAR. iscompleted) {system. threading. thread. sleep (0);} int n = program. form. webform. count; program. form. webform [u]. navigate (U); system. threading. thread. sleep (timespan. fromminutes (1); string strhtml = program. form. webform [u]. strhtmlleng; // output HTML to the local edisk. When collecting information, you can directly operate the HTML system. io. streamwriter Sw = new streamwriter (string. format ("E:/1_02.16.txt", u); Sw. write (string. format ("Start Time: {0} \ r \ n End Time: {1} \ r \ n open information: {2}", DT. tostring ("G"), datetime. now, strhtml); Sw. dispose ();}

Now we can use multiple threads to execute the openwebpage method to simulate multi-threaded webborwser operations.

Here is the small program I tested and written.

Http://files.cnblogs.com/jiangming/TestThreadWebBrowser.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.