2011-03-11 84 views
0

我想做網絡瀏覽器機器人。它應該點擊鏈接並等待25秒。C#網絡瀏覽器機器人問題

private void webBrowserMain_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) // This is only way It worked for me. 
    { 
     if (webBrowserMain.Url.AbsoluteUri == @"http://www.clix-cents.com/pages/clickads") 
     { 
      Regex regAddId = new Regex("onclick=\\'openad\\(\"([\\d\\w]+)\"\\);", RegexOptions.IgnoreCase); // Find link and click it. 
      if (regAddId.IsMatch(webBrowserMain.DocumentText)) 
      { 
       string AddId = regAddId.Match(webBrowserMain.DocumentText).Groups[1].ToString(); 
       webBrowserMain.Navigate(@"http://www.clix-cents.com/pages/clickads?h=" + AddId); 
      } 
     } 
     else if (webBrowserMain.Url.AbsoluteUri.Contains("http://www.clix-cents.com/pages/clickads?h=")) // up to there everything is ok. But problem starts here. 
     { 
      Thread.Sleep(25000); // It pouses whole thread and browser, so timer in browser is not counting down. 
      Regex regCaptchaCode = new Regex("src=\\'/pages/captcha\\?t=c&s=([\\d\\w\\W]+)\\'", RegexOptions.IgnoreCase); 
      if (regCaptchaCode.IsMatch(webBrowserMain.DocumentText)) 
      { 
       pictureBox1.ImageLocation = @"http://www.clix-cents.com/pages/captcha?t=c&s=" + regCaptchaCode.Match(webBrowserMain.DocumentText).ToString(); 
      } 
     } 
    } 

如何爲這樣的東西寫bot?我不知道。

回答

3

不要重新發明車輪 - 已有解決方案,例如WatiN,它主要用於測試,但也適用於自動化。

從華廷頁面代碼示例:

[Test] 
public void SearchForWatiNOnGoogle() 
{ 
    using (var browser = new IE("http://www.google.com")) 
    { 
    browser.TextField(Find.ByName("q")).TypeText("WatiN"); 
    browser.Button(Find.ByName("btnG")).Click(); 

    Assert.IsTrue(browser.ContainsText("WatiN")); 
    } 
} 
1

您可能可以使用計時器。例如:

private Timer t = new Timer(); 
private string nextUrl = ""; 
private void buttonStart_Click(object sender, EventArgs e) 
{ 
    t.Interval = 2500; 
    t.Tick += new EventHandler(t_Tick); 
} 

void t_Tick(object sender, EventArgs e) 
{ 
    if (!string.IsNullOrEmpty(nextUrl)) 
     webBrowser1.Navigate(nextUrl); 
    else 
    { 
     Regex regCaptchaCode = new Regex("src=\\'/pages/captcha\\?t=c&s=([\\d\\w\\W]+)\\'", RegexOptions.IgnoreCase); 
     if (regCaptchaCode.IsMatch(webBrowserMain.DocumentText)) 
     { 
      pictureBox1.ImageLocation = @"http://www.clix-cents.com/pages/captcha?t=c&s=" + regCaptchaCode.Match(webBrowserMain.DocumentText).ToString(); 
     } 
    } 
} 
private void webBrowserMain_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) // This is only way It worked for me. 
{ 
    if (webBrowserMain.Url.AbsoluteUri == @"http://www.clix-cents.com/pages/clickads") 
    { 
     Regex regAddId = new Regex("onclick=\\'openad\\(\"([\\d\\w]+)\"\\);", RegexOptions.IgnoreCase); // Find link and click it. 
     if (regAddId.IsMatch(webBrowserMain.DocumentText)) 
     { 
      string AddId = regAddId.Match(webBrowserMain.DocumentText).Groups[1].ToString(); 
      nextUrl = @"http://www.clix-cents.com/pages/clickads?h=" + AddId; 
      t.Start(); 
     } 
    } 
    else if (webBrowserMain.Url.AbsoluteUri.Contains("http://www.clix-cents.com/pages/clickads?h=")) // up to there everything is ok. But problem starts here. 
    { 
     nextUrl = ""; 
     t.Start(); 
    } 
} 

實際的實施將取決於網站上的實際數據以及如何使用它。如果所有鏈接都在一個頁面上,並且您想打開每個鏈接,則可以解析所有鏈接並存儲到列表中。然後啓動計時器。在每個Tick中,您可以打開1個項目。